答案 0 :(得分:3)
单击索引标题时,可以使用tableView(_:sectionForSectionIndexTitle:at:)方法返回相应的部分索引。
let sectionArr = ["a","a","b","c","d","d"]
func numberOfSections(in tableView: UITableView) -> Int {
return sectionArr.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionArr[section]
}
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return Array(Set(sectionArr))//["a","b","c","d"]
}
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
if let index = sectionArr.firstIndex(where: { $0 == title }) {
return index//0 for a, 2 for b, 3 for c, 4 for d
}
return 0
}
答案 1 :(得分:0)
您可以使用自定义视图作为UITableView的索引视图,如下面的参考所示:
Custom UITableView section index
这是在Objective-c中,但是我认为这是一个很好的参考,因为它具有touchesMoved
,touchesEnded
,touchesCancelled
和ActiveWorkbook
事件,您可以设置您想要的用户体验。另外,在此自定义索引视图中添加自定义行为也非常容易。
希望这会有所帮助。