Abhinav
Abhinav

Reputation: 38162

Section index in table view

I am implementing a table index view and amazed to see how my table indexes are working even without implementing:

 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index method.

I have only implemented:

 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 

Strangely, when I am running in breakpoints, Once i click on any of the index values my

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

method is getting called.

Any clue why this is so happening and what is the significance of sectionForSectionIndexTitle method then.

Upvotes: 1

Views: 1002

Answers (2)

cV2
cV2

Reputation: 5319

if you have a list of all letters in alphabet and your list only contains some entries, you could use the following code:

//Asks the data source to return the index of the section having the given title and section title index. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {

if (tableView == self.searchDisplayController.searchResultsTableView || self.searchBar.text.length > 0)
{
    return 0;
}
else
{

    //direct - firsttime match
    if([self.realMIndexArray containsObject:title]) {
        NSInteger count = 0;
        for(NSString *character in self.realMIndexArray)
        {
            if([character isEqualToString:title]){
                return count;
            }
            count ++;
        }
    }
    else {

        //take next higher letter from alphabet and check if its contained in the "available letters list"
        //if not, select last entry of list
        for(int i = [self.indexArray indexOfObject:title] + 1; i < [self.indexArray count]; i++) {

            NSString* character = [self.indexArray objectAtIndex:i];

            if([self.realMIndexArray containsObject:character]) {

                return [self.realMIndexArray indexOfObject:character];

            }

        }

        return [self.realMIndexArray count] - 1;

    }
    return 0;// in case of some eror donot crash d application

}

}

realMIndexArray count == letters really existing in list indexArray = list of all letters in alphbeth.

hope this helps someone (took me a little bit of time to figure it out)

Upvotes: 1

Shaggy Frog
Shaggy Frog

Reputation: 27601

Any clue why this is so happening

Yes. When you tap (you do not click on an iPhone) on a table's index, the underlying table view will want to jump to that section, and the cells in that section. In order to do that, it has to ask the data source for those cells so it can render them on the screen.

what is the significance of sectionForSectionIndexTitle method then.

The documentation for tableView:sectionForSectionIndexTitle:atIndex: (which is an optional method in the UITableViewDataSource protocol) says:

Asks the data source to return the index of the section having the given title and section title index.

and

You implement this method only for table views with a section index list—which can only be table views created in the plain style (UITableViewStylePlain).

Does this apply for your UITableView? In other words, are you using a grouped table view style?

Upvotes: 0

Related Questions