Saturday, June 8, 2013

UITableViewDataSource tableView sectionForSectionIndexTitle atIndex example in Objective C (iOS).


UITableViewDataSource tableView sectionForSectionIndexTitle atIndex

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

Parameters of [UITableViewDataSource tableView sectionForSectionIndexTitle atIndex]
tableView
The table-view object requesting this information.
title
The title as displayed in the section index of tableView.
index
An index number identifying a section title in the array returned by sectionIndexTitlesForTableView:.

Return Value of [UITableViewDataSource tableView sectionForSectionIndexTitle atIndex]
An index number identifying a section.

Discussion of [UITableViewDataSource tableView sectionForSectionIndexTitle atIndex]
This method is passed the index number and title of an entry in the section index list and should return the index of the referenced section. To be clear, there are two index numbers in play here: an index to an section index title in the array returned by sectionIndexTitlesForTableView:, and an index to a section of the table view; the former is passed in, and the latter is returned. 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). Note that the array of section titles returned by sectionIndexTitlesForTableView: can have fewer items than the actual number of sections in the table view.

UITableViewDataSource tableView sectionForSectionIndexTitle atIndex example.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [[fetchedResultsController sections] count];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [fetchedResultsController sectionIndexTitles];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

Example of [UITableViewDataSource tableView sectionForSectionIndexTitle atIndex].
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{

// as the search icon is at index 0, scroll to the top
    if(index == 0)   
        [tableView scrollRectToVisible:CGRectMake(0, 0, searchBar.width, searchBar.height) animated:YES];   
    return index -1;
}

UITableViewDataSource tableView sectionForSectionIndexTitle atIndex example.
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    // Find the correct index of cell should scroll to.
    int foundIndex = 0;
    for (Object *obj in dataArray) {
        if ([[[obj.YOURNAME substringToIndex:1] uppercaseString] compare: title] == NSOrderedDescending)
            break;
        foundIndex++;
    }
    if(foundIndex > dataArray.count)
        foundIndex = dataArray.count;
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:foundIndex inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];

    return 1;
}

End of UITableViewDataSource tableView sectionForSectionIndexTitle atIndex example article.