简体   繁体   English

在Swift中用数组中的多个部分和多个字典填充TableView

[英]Populating TableView with multiple sections and multiple dictionary in an array in Swift

I have a 3 category which I was using as a section. 我有一个3类,我用作一个部分。 In that section I have to populate data which is in array of dictionary. 在该部分中,我必须填充字典数组中的数据。 Here is my code:- 这是我的代码: -

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
}

The items array I need to populate into table category wise. 我需要将items数组填充到表类别中。 If anyone can answer. 如果有人可以回答。 Thank you! 谢谢!

Use a custom struct 使用自定义结构

struct Category {
   let name : String
   var items : [[String:Any]]
}

var sections = [Category]()

let itemsA = [["Item": "item A","ItemId" : "1"],["Item": "item B","ItemId" : "2"],["Item": "item C","ItemId" : "3"]]
let itemsB = [["Item": "item A","ItemId" : "1"],["Item": "item B","ItemId" : "2"],["Item": "item C","ItemId" : "3"]]
let itemsC = [["Item": "item A","ItemId" : "1"],["Item": "item B","ItemId" : "2"],["Item": "item C","ItemId" : "3"]]


sections = [Category(name:"A", items:itemsA), 
            Category(name:"B", items:itemsB), 
            Category(name:"C", items:itemsC)]

func numberOfSections(in tableView: UITableView) -> Int {
    return self.sections.count
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return self.sections[section].name
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    let items = self.sections[section].items
    return items.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "StoreCell") as! UITableViewCell
    let items = self.sections[indexPath.section].items
    let item = items[indexPath.row]
    print(item["ItemId"] as? String)

    return cell
}

You can still improve the code if you are using a custom struct also for the dictionaries. 如果您还为字典使用自定义结构,则仍可以改进代码。

If itemA array for Category A, itemB array for Category B and so on then you can return array count in numberOfRowsInSection this way. 如果类别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
}

Note: It is batter if you create single Array of struct or custom class that will reduce all your array with single array. 注意:如果您创建单个Array结构或自定义类,它将减少单个数组的所有数组,那就更好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM