UITableView section index spacing on iOS 7

前端 未结 7 1625
再見小時候
再見小時候 2020-12-28 14:55

On iOS 6, my UITableView\'s section index would distribute all of its items evenly across the height of the table view. In iOS 7 all of the items are clumped together in the

相关标签:
7条回答
  • 2020-12-28 15:27

    Here is something I played with and works. It follows Anton M's advice, uses Swift 2 and ios 9.

    Let's say you have the following section indexes:

    let indexes: [String] = ["A", "B", "C", "D", "E"]
    

    So that everyone is on the same page, A is at index 0. B is at index 1 and so on.

    Lets assume you want two spaces between indexes. Then define your section indexes as:

    let indexes: [String] = ["A", "", "", "B", "", "", "C", "", "", "D", "", "", "E"]
    

    So now, A is still at index 0 but B is now at index 3, C is at 6, D is at 9 and E is at 12.

    All you need is to figure out the offset in the sectionForSectionIndexTitle. What you are after is the index of a real title as this is what you have in your table. Therefore:

    override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {        
            return index / 3
    }
    

    So, for every extra space you need between the indexes you need to add 1 to the enumerator of the above expression. You can easily test this in Playground.

    0 讨论(0)
提交回复
热议问题