我的总体目标是创建一个UITableView,它根据字典中的值包含字母部分标题。
我有一个基本对象,其中包含水果的名称和描述。我希望每个水果的名称都显示在UITableCell上,在节标题的相应字母下面。
最终,tableview看起来像这样:(其中C P S是章节标题)
C------------
Clementine
Cherry
Coconut
P------------
Pear
Peach
S------------
Strawberry
这是我的数据:
var fruitModel = [String:Any]()
fruitModel = [
"C" : [
FruitModel(name: "Clementine", description: "Clementines are super tasty!"),
FruitModel(name: "Cherry", description: "Cherry's are red."),
FruitModel(name: "Coconut", description: "Nuts for coconuts!!!"),
],
"P" : [
FruitModel(name: "Pear", description: "Pears rock!"),
FruitModel(name: "Peach", description: "Mmmm, Peach."),
],
"S" : [
FruitModel(name: "Strawberry", description: "A widely grown hybrid species of the genus Fragaria. It is cultivated worldwide for its fruit.")
]
]
让我失望的部分是访问字典值中的名称字符串,并将其显示在cellForRowAt中。下面的代码是我被困的地方:
cellForRowAt:
var allValues = Array(fruitModel.values)
cell.fruitNameLabel.text = (allValues[indexPath.row] as AnyObject).name // Cannot subscript a value of type 'inout Array<Any>'
一旦我弄清楚如何在每个单元格中显示水果名称,我就能够使章节标题有效。我只需要帮助解决我的错误。感谢。
答案 0 :(得分:1)
有关索引的错误,请参阅Swift Dictionary: Get values as array。
但是,由于字典值不能保证按照添加顺序返回,因此您将面临更大的问题。
字典存储相同类型的键与集合中相同类型的值之间的关联,但没有定义的顺序。
因此,如果您希望按字母顺序显示数据结构,则需要重新考虑所使用的数据结构。
答案 1 :(得分:0)
在表视图中以 swift
显示字典值数组var arrayDictionary = [[String:Any]]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellTable = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cellTable.labelTitle.text = arrayDictionary[indexPath.row]["name"] as? String
cellTable.labelAuthor.text = arrayDictionary[indexPath.row]["region"] as? String
cellTable.labelDescription.text = arrayDictionary[indexPath.row]["subregion"] as? String
return cellTable
}
答案 2 :(得分:0)
此处是您查询的完整示例
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
struct FruitModel {
var name: String
var description: String
}
var fruitModel = [String: [FruitModel]]()
var dataKeys : Array = [String]()
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.delegate = self
tableView.dataSource = self
fruitModel = [
"C" : [
FruitModel(name: "Clementine", description: "Clementines are super tasty!"),
FruitModel(name: "Cherry", description: "Cherry's are red."),
FruitModel(name: "Coconut", description: "Nuts for coconuts!!!"),
],
"P" : [
FruitModel(name: "Pear", description: "Pears rock!"),
FruitModel(name: "Peach", description: "Mmmm, Peach."),
],
"S" : [
FruitModel(name: "Strawberry", description: "A widely grown hybrid species of the genus Fragaria. It is cultivated worldwide for its fruit.")
]
]
dataKeys = (fruitModel as NSDictionary).allKeys as! [String]
print(dataKeys)
tableView.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return dataKeys.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let sec = dataKeys[section]
return fruitModel[sec]?.count ?? 0
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return dataKeys[section]
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
let sec = dataKeys[indexPath.section]
cell.textLabel?.text = fruitModel[sec]?[indexPath.row].name
return cell;
}
}