gpt4 book ai didi

ios - 在 sectionForSectionIndexTitle 方法中使用 A-Z 在 UITableView 中索引部分标题

转载 作者:行者123 更新时间:2023-11-28 15:44:22 24 4
gpt4 key购买 nike

我的 UITableView 中有数百个部分。我已将所有部分放在一个数组中,并将该数组更改为仅包含大写字母并删除了所有重复字符。

//sample array
let sectionsArray: [String] = [Apple, Asparagus, Banana, Barley, Cucumber...]

var firstLettersOfSections: [String] = []

firstLettersOfSections = sectionsArray.map { String($0.characters.first!).uppercased() }
firstLettersOfSections = removeDuplicates(&firstLettersOfSections)

我返回了 firstLettersOfSectionssectionIndexTitles方法:

override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return firstLettersOfSections
}

此代码在 tableView 的右侧生成正确的字母数字字符,但是,当点击这些字符时,它们会转到与 firstLettersOfSections 的索引相对应的部分索引。 .

我在 sectionForSectionIndexTitle 中放了什么? tableView 方法,以便当我点击垂直列表中的字母数字字符时,它会转到具有指定字母或数字的第一部分,并且索引不区分大小写?

最佳答案

我所要做的就是在 sectionsArray 中找到第一个项目的索引,该索引符合以下条件:项目的第一个大写字母等于在表格 View 的部分索引:

override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
return sectionsArray.index(where: { String($0.characters.first!).uppercased() == title })
}

关于ios - 在 sectionForSectionIndexTitle 方法中使用 A-Z 在 UITableView 中索引部分标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43333749/

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