gpt4 book ai didi

ios - 从 JSON 数据 Swift 4 创建 TableView 部分

转载 作者:搜寻专家 更新时间:2023-11-01 06:13:21 26 4
gpt4 key购买 nike

我的 JSON 数据文件已经按字母顺序排列并正确显示在我的 TableView 中。我使用以下方法设置章节标题:

struct FigureStats: Decodable {
let name: String
let number: String
let year: String?
}

var figSectionTitles = [String]()
var figures = [FigureStats]()

override func viewDidLoad() {
super.viewDidLoad()
figSectionTitles = [String](arrayLiteral: "#", "A", "B", "C")
}

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

func tableView( tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return figures.count
}

func downloadJSON(completed: @escaping () -> ()) {
let url = URL(string: "https://xxxx")

URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error == nil {
do {
self.figures = try JSONDecoder().decode([FigureStats].self, from: data!)
DispatchQueue.main.async {
completed()
}
} catch {
print("JSON Error")
}
}
}.resume()
}

问题是所有条目在每个部分内重复,而不是根据每个名称的第一个字符落入正确的部分。

如何获取每个名称的第一个字符,然后将这些条目归入正确的部分?

最佳答案

您可以使用 init(grouping:by:)将数组分组到字典中。

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

var figures = [Employee]()
var figuresByLetter = [(key: String, value: [Employee])]()
override func viewDidLoad() {
super.viewDidLoad()
figures = [Employee(name: "Kofi", year: 2000, id: 1),Employee(name: "Abena", year: 2002, id: 2),Employee(name: "Efua", year: 2003, id: 3),Employee(name: "Kweku", year: 2003, id: 3),Employee(name: "Akosua", year: 2003, id: 3)]
figuresByLetter = Dictionary(grouping: figures, by: { String($0.name.trimmingCharacters(in: .whitespaces).first!) }).sorted(by: { $0.0 < $1.0 })
print(figuresByLetter)
}
func numberOfSections(in tableView: UITableView) -> Int {
return figuresByLetter.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return figuresByLetter[section].key
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return figuresByLetter[section].value.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") ?? UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = figuresByLetter[indexPath.section].value[indexPath.row].name
return cell
}


}
struct Employee {
var name:String?
var year:Int?
var id:Int?
}

分组后的数据

[(key: "A", value: [Employee(name: "Abena", year: 2002, id: 2), Employee(name: "Akosua", year: 2003, id: 3)]),
(key: "E", value: [Employee(name: "Efua", year: 2003, id: 3)]),
(key: "K", value: [Employee(name: "Kofi", year: 2000, id: 1), Employee(name: "Kweku", year: 2003, id: 3)])]

enter image description here

关于ios - 从 JSON 数据 Swift 4 创建 TableView 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50427800/

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