UITableview节索引单击事件

时间:2019-06-03 09:45:48

标签: ios uitableview

有什么方法可以获取表格视图节索引的点击事件?

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

我想在下图中获得此蓝色标记索引的点击事件。

enter image description here

2 个答案:

答案 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中,但是我认为这是一个很好的参考,因为它具有touchesMovedtouchesEndedtouchesCancelledActiveWorkbook事件,您可以设置您想要的用户体验。另外,在此自定义索引视图中添加自定义行为也非常容易。

希望这会有所帮助。

相关问题