在Swift中用数组中的多个部分和多个字典填充TableView [英] Populating TableView with multiple sections and multiple dictionary in an array in Swift

查看:120
本文介绍了在Swift中用数组中的多个部分和多个字典填充TableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3类,我用作一个部分。在该部分中,我必须填充字典数组中的数据。这是我的代码: -

  var sections = [类别A,类别B,类别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) - >串? {
return self.sections [section]
}

func tableView(_ tableView:UITableView,numberOfRowsInSection section:Int) - > Int {
return ??
}

func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell {
让cell = tableView.dequeueReusableCell(withIdentifier:StoreCell)为! UITableViewCell
????
????
返回单元格
}

我需要填充到表类别中的items数组明智的。如果有人可以回答。谢谢!

解决方案

如果是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 {
让cell = tableView.dequeueReusableCell(withIdentifier:StoreCell)为! UITableViewCell
开关(indexPath.section){
案例0:
//访问项A [indexPath.row]
案例1:
//访问itemsB [indexPath。行]
默认值:
//访问itemsC [indexPath.row]
}
返回单元格
}

注意:如果您创建单个Array结构或自定义类,它将减少单个数组的所有数组。 p>

I have a 3 category which I was using as a section. 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. If anyone can answer. Thank you!

解决方案

If itemA array for Category A, itemB array for Category B and so on then you can return array count in numberOfRowsInSection this way.

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.

这篇关于在Swift中用数组中的多个部分和多个字典填充TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆