Luis Ramirez
Luis Ramirez

Reputation: 1634

TableView sectionForSectionIndexTitle

Okay I wanted to know how would you use this method and when should you use it. I check apple documentation, but couldn't really understand.

I am confused to what code would go into this method.

Would really appreciate an example.

Upvotes: 0

Views: 353

Answers (1)

Paulw11
Paulw11

Reputation: 114992

This function is effectively used to tell the tableview which section to scroll to when the user taps an entry in the section index that you can display to the right of the tableview (The blue letters in the image below)

enter image description here

Since there may be a different number of sections in your tableview and in your index, you need to provide a mapping. This is done via sectionForSectionIndexTitle

Consider the image above; the index contains the letters 'A'-'Z', but there may not be any students with names starting with some letters. Let's assume that there is a students with names starting with A, B and D and F. The number of sections in the table would be 4 (The number of populated name "lists") but there are 26 sections in the section index.

If the user taps "A" then sectionForSectionIndexTitle needs to return 0 (The A section). For B it would return 1 (The B section). For "C" it could return 2 (The section after the 'missing' C section). For D it would return 2. For any other letters it could return 3; the last section in the table.

Upvotes: 1

Related Questions