Ich habe ein bisschen ein Polymorphie-Problem. Ich habe drei Arrays aList, bList und cList, die verschiedene Arten von Ausrüstung "A", "B", "C" enthalten.Wie kann ich mehrere Tabellenansichtsabschnitte füllen, wenn die Inhalte der Arrays unterschiedliche Typen haben?
Ich möchte einen Tableviewcontroller mit 3 Abschnitten für aList, bList, cList bevölkern. Aber ich bin zu kämpfen ein wenig, wenn ich zu bekommen:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
Das Problem, das ich habe, ist, dass im Idealfall i drei Arrays innerhalb eines Arrays zum Beispiel haben würde. Abschnitt = [aList, bList, cList).
In meinem Singleton "A", "B", "C" sind Strukturen und repräsentieren eine Art von Ausrüstung und so habe ich sie in Klassen und Unterklassen von einer "Equipment" Oberklasse und dann versucht, ein Array von zu erstellen die Oberklasse "Equipment" aber das hat auch nicht funktioniert.
Beratung wäre super.
class EquipmentListController: UITableViewController {
var model = SingletonModel.sharedInstance
var aList:[SingletonModel.A] = []
var bList:[SingletonModel.B] = []
var cList:[SingletonModel.C] = []
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
aList = model.AList
bList = model.BList
cList = model.CList
sections.append(aList)
sections.append(bList)
sections.append(cList)
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section{
case 1:
self.tableView.rowHeight = 70
return aList.count
case 2:
return bList.count
case 3:
return cList.count
default:
return 0
}
}
Mein Problem ist, unten mit der Linie lassen Ausrüstung: SingletonModel.A (oder B oder C) - im Grunde i drei separate Arrays hat und jeweils eine andere Art.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
guard let cell = tableView.dequeueReusableCell(withIdentifier: "equipmentcell", for: indexPath) as? EquipmentCell else
{
}
let equipment: SingletonModel.A (or B or C) = changeDataSource(indexPath: indexPath as NSIndexPath)
cell.equipmentTitle.text = equipment.equipmentName
cell.accessoryType = .disclosureIndicator
return cell
}
func changeDataSource(indexPath: NSIndexPath) -> SingletonModel.A, B or C
{
var equipment:SingletonModel.A (B or C)
equipment = A, B or C [indexPath.row]
return equipment
}
func tableView(tableView: UITableView!, titleForHeaderInSection section: Int) -> String!
{
switch section
{
case 1:
return "A"
case 2:
return "B"
case 3:
return "C"
default:
return nil
}
}
override func tableView(_ tableView: UITableView,
heightForHeaderInSection section: Int) -> CGFloat
{
return 40
}
}
Dank
Hallo. Vielen Dank. Kannst du mir bitte erklären, indexPath.section == 0? 70,0: 50,0; Prost – Mike
@Mike Sie hatten diese Zeile in 'Fall 1' Ihres Codes:' self.tableView.rowHeight = 70' Das ist falsch - Sie sollten Zeilenhöhe nicht innerhalb eines Delegaten setzen. Wenn Sie die Zeilenhöhe des oberen Abschnitts auf 70 setzen und die restlichen Abschnitte auf 50 festlegen müssen, geben Sie eine Überschreibung für die Funktion "heightForRowAt: indexPath" an. – dasblinkenlight