ios 在 Swift 中的数组中使用多个部分和多个字典填充 TableView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42552611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Populating TableView with multiple sections and multiple dictionary in an array in Swift
提问by shahin ali agharia
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:-
我有一个 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
}
The items array I need to populate into table category wise. If anyone can answer. Thank you!
我需要填充到表格类别中的 items 数组。如果有人能回答。谢谢!
回答by vadian
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.
如果您也为字典使用自定义结构,您仍然可以改进代码。
回答by Nirav D
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.
注意:如果您创建单个结构数组或自定义类,它将使用单个数组减少所有数组,那将是面子。