iPhone UITableView. How do turn on the single letter alphabetical list like the Music App?

后端 未结 9 929
再見小時候
再見小時候 2020-12-02 04:25

In the iPhone music app, selecting Artist, Songs, or Albums presents a tableView with a verticl list of single letters at the righthand side of the UI that enables rapid scr

9条回答
  •  醉酒成梦
    2020-12-02 05:06

    Here's a simple solution in Swift, assuming you have your title headers in an array. If the title couldn't be found, it will return the previous index in the array.

    func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
        return "ABCDEFGHIJKLMNOPQRSTUVWXYZ".characters.flatMap{String($0)}
    }
    
    func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
        return self.headerTitles.filter{$0 <= title}.count - 1
    }
    

提交回复
热议问题