我有3个类别,我一直将其用作一个部分。在那部分中,我必须填充字典数组中的数据。这是我的代码:
var sections = [Category A, Category B, Category C]
var itemsA = [["Item": "item A","ItemId" : "1"],["Item": "item B","ItemId" : "2"],["Item": "item C","ItemId" : "3"]]
var itemsB = [["Item": "item A","ItemId" : "1"],["Item": "item B","ItemId" : "2"],["Item": "item C","ItemId" : "3"]]
var itemsC = [["Item": "item A","ItemId" : "1"],["Item": "item B","ItemId" : "2"],["Item": "item C","ItemId" : "3"]]
func numberOfSections(in tableView: UITableView) -> Int {
return self.sections.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sections[section]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ??
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "StoreCell") as! UITableViewCell
????
????
return cell
}
我需要将项目数组明智地填充到表类别中。如果有人可以回答。谢谢!
如果类别A的itemA数组,类别B的itemB数组等等,那么您可以通过numberOfRowsInSection
这种方式返回数组计数。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch (section) {
case 0:
return itemsA.count
case 1:
return itemsB.count
default:
return itemsC.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "StoreCell") as! UITableViewCell
switch (indexPath.section) {
case 0:
//Access itemsA[indexPath.row]
case 1:
//Access itemsB[indexPath.row]
default:
//Access itemsC[indexPath.row]
}
return cell
}
注意:如果您创建结构或自定义类的单个数组,这将使您的所有数组都减少为单个数组。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句