gpt4 book ai didi

ios - 如何使用字典将行添加到 TableView 中的特定部分?

转载 作者:行者123 更新时间:2023-11-29 00:47:05 24 4
gpt4 key购买 nike

我有一本字典,正在从我的数据库中导入信息。我需要将它专门放在表格 View 的正确部分。提供了所有信息,如果您需要更多详细信息或代码,我会提供。

字典输出 ["March 27": ["do the dishes", "take out the trash"], "March 29": ["walk the dog", "Water the plants"], "March 28": ["打扫房间"]]

var date = ["March 27", "March 28", "March 29"]

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

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

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCell(withIdentifier: "cell") as! Chores
//Having trouble on what to do here
return cell
}

最佳答案

这就是你可以做的,我认为这是不言自明的:

var output = ["March 27": ["do the dishes", "take out the trash"], "March 29": ["Walk the dog", "Water the plants"], "March 28": ["Clean the house"]]
var date = Array(output.keys)

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

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return output[date[section]]?.count ?? 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCell(withIdentifier: "cell") as! Chores
var value = output[date[indexPath.section]]?[indexPath.row] ?? ""
cell.textLabel.text = value
return cell
}

//已编辑的行数:

output[date[section]]?.count

就是这样,这主要是给你可选项,但在这个例子中我会忽略它:

let keyForSection = date[section]
let arrayOfStringsForKey = output[keyForSection]
let numberOfRows = arrayOfStringsForKey.count

你做了类似的事情来获取实际值,但你传递的不是计数,而是你想要从中获取值的行的索引

let value = arrayOfStringsForKey[rowNumber]

关于ios - 如何使用字典将行添加到 TableView 中的特定部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38520771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com