开发者

What exactly is the point of tableView:sectionForSectionIndexTitle:atIndex:?

开发者 https://www.devze.com 2022-12-27 03:35 出处:网络
The documentation is not really clear开发者_运维百科 about this. Why is the UITableViewDataSource asked to provide the section index for an index-bar section title?

The documentation is not really clear开发者_运维百科 about this. Why is the UITableViewDataSource asked to provide the section index for an index-bar section title?

I mean... what use cases make this method important? When is this called and why?


Basically, it defines which section the table view scrolls to when you click on the specified section index title. Let's say you define your index titles as the letters A through Z, but you don't actually have items for each letter. In -tableView:sectionForIndexTitle:atIndex:, you would specify the next section that has an item.


This website has a good answer :

sectionIndexTitlesForTableView: In order to properly render the index, the UITableView needs to know all index titles to display. This method returns an array of strings containing all indices. In our case, A, B, C, D, etc.

sectionForSectionIndexTitle: When you start scrolling through the index, the table needs to know how far down/up to scroll so that the letter you're touching corresponds the appropriate section in the table. This method returns an index value (integer) of the section you're currently touching in the index.


The method you asked about "-tableView:sectionForSectionIndexTitle:atIndex:" is connecting 2 (below) related lists, regarding UITableView plain style data population.

1st list: TableView-Section-Title-List 《TVSTL》, in "Contacts App", it is composed/sorted by peoples LastNames. It is the section-headers appear on the left side of the screen.

2nd list: Indexed-List-Titles 《ILT》, normally shown on the right hand side of screen, it could be an arbitrary group of indexes about section-headers above. This list is for user to tap, it works like scrolling bar.

iOS documentation is not very clear about these 2 lists, because 《TVSTL》 current listing can be bigger OR smaller than 《ILT》, there is no restrictions on which list is longer.

And "Contacts APP" is a bad example to show this critical point...unless you have very few people's names (let's say less than 10), or if you have 中文Chinese-character people names, which have no titles, but are grouped into sections, and those sections can NOT be indexed.

Therefore, when those 2 lists are not match, "-tableView:sectionForIndexTitle:atIndex:" is providing a solution about TableView's next scrolling movement.

Apple's doc on "Populating an Indexed List" explains its suggested implementation in details, but it is bit cumbersome (using 4 For-loops, 5 Arrays). If you don't want "Creating a Table View Programmatically", you may skip those code, otherwise, they are in Listing 4-5, Listing 4-6, Listing 4-7, Listing 4-8, Listing 4-9 of Table View Programming Guide for iOS

0

精彩评论

暂无评论...
验证码 换一张
取 消