UITableview 部分索引点击事件

Ahe*_*agh 2 uitableview ios

有没有办法获取表视图部分索引的点击事件?

我进行了很多研究,但没有得到任何合适的建议。有谁知道如何获取索引的点击事件操作?

我想获取下图中这个蓝色标记索引的点击事件。

在此输入图像描述

Raj*_*r R 5

您可以使用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
}
Run Code Online (Sandbox Code Playgroud)