Arrays 如何使用sectionIndexTitles的不同数组填充索引UITableView的节

Arrays 如何使用sectionIndexTitles的不同数组填充索引UITableView的节,arrays,swift,uitableview,Arrays,Swift,Uitableview,我正在使用人名数组加载一个表。我想根据每个人的居住国将这些部分分开,同时将sectionIndexTitles限制在州名的第一个字母内。我的SECTIONINDEXTITLE将是,即[A”,“C”,“D”,“F”,“G”,“H”,“i”,“K”,“L”,“M”,“N”,“O”,“P”,“R”,“S”,“T”,“U”,“V”,“W”],而表格部分将被分为所有50个状态 本质上,字母A(索引0)属于阿拉斯加、阿拉巴马州、阿肯色州和亚利桑那州。没有B,因此下一个字母C(索引2)应该滚动到加利福尼亚州,

我正在使用人名数组加载一个表。我想根据每个人的居住国将这些部分分开,同时将sectionIndexTitles限制在州名的第一个字母内。我的SECTIONINDEXTITLE将是,即[A”,“C”,“D”,“F”,“G”,“H”,“i”,“K”,“L”,“M”,“N”,“O”,“P”,“R”,“S”,“T”,“U”,“V”,“W”],而表格部分将被分为所有50个状态

本质上,字母A(索引0)属于阿拉斯加、阿拉巴马州、阿肯色州和亚利桑那州。没有B,因此下一个字母C(索引2)应该滚动到加利福尼亚州,该州的分区计数为4


我遇到的问题是,这些部分显然没有正确索引,因此,当点击除字母A以外的任何索引标题时,tableview不会滚动到正确的字母

根据州名称对人物数组进行分组,并从字典中创建元组数组。然后在tableview数据源方法中使用该数组

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    struct Person {
        var name: String
        var state: String
    }
    var allPeople = [Person]()
    var groupedPeople = [(state:String, people:[Person])]()
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        allPeople = [Person(name: "a", state: "Alaska"), Person(name: "x", state: "Florida"),
                     Person(name: "c", state: "California")]
        groupedPeople = Dictionary(grouping: allPeople, by: { $0.state }).sorted(by: { $0.key < $1.key })
            .map({ (state:$0.key, people:$0.value)
            })
    }
    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return Array(Set(groupedPeople.map({ String($0.state.first!) })))
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return groupedPeople.count
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return groupedPeople[section].state
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return groupedPeople[section].people.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") ?? UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
        cell.textLabel?.text = groupedPeople[indexPath.section].people[indexPath.row].name
        cell.detailTextLabel?.text = groupedPeople[indexPath.section].people[indexPath.row].state
        return cell
    }
}

按状态名称对人员数组进行分组,并从字典中创建元组数组。然后在tableview数据源方法中使用该数组

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    struct Person {
        var name: String
        var state: String
    }
    var allPeople = [Person]()
    var groupedPeople = [(state:String, people:[Person])]()
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        allPeople = [Person(name: "a", state: "Alaska"), Person(name: "x", state: "Florida"),
                     Person(name: "c", state: "California")]
        groupedPeople = Dictionary(grouping: allPeople, by: { $0.state }).sorted(by: { $0.key < $1.key })
            .map({ (state:$0.key, people:$0.value)
            })
    }
    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return Array(Set(groupedPeople.map({ String($0.state.first!) })))
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return groupedPeople.count
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return groupedPeople[section].state
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return groupedPeople[section].people.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") ?? UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
        cell.textLabel?.text = groupedPeople[indexPath.section].people[indexPath.row].name
        cell.detailTextLabel?.text = groupedPeople[indexPath.section].people[indexPath.row].state
        return cell
    }
}

你有人物对象数组吗?显示tableview数据源方法、人员对象。我没有添加方法,因为它有很多与问题无关的额外内容。搜索栏,分段控件,可在按姓氏排序或按州名排序之间切换。我不想让事情变得比需要的更复杂。如果你愿意,我可以寄给你。我只是不想把问题弄得一团糟。你检查过我的答案了吗。你明白吗?是的,我明白。它工作得很好,但在sectionIndexTitles中给了我重复的字母。这创建了一个无序的sectionIndexTitles数组,点击字母不能正常滚动。原版更好。你有人物对象数组吗?显示tableview数据源方法、人员对象。我没有添加方法,因为它有很多与问题无关的额外内容。搜索栏,分段控件,可在按姓氏排序或按州名排序之间切换。我不想让事情变得比需要的更复杂。如果你愿意,我可以寄给你。我只是不想把问题弄得一团糟。你检查过我的答案了吗。你明白吗?是的,我明白。它工作得很好,但在sectionIndexTitles中给了我重复的字母。这创建了一个无序的sectionIndexTitles数组,点击字母不能正常滚动。原版比较好。