You must be familiar with 'sectionIndexTitlesForTableView' method which allows you to define a list of shortcuts for your sections in an UITableView (like the iPhone contacts app). What if you want a similar behaviour without having any sections? This is how you could tweak apple's default behaviour of 'sectionIndexTitlesForTableView'. Implement the following methods in the delegate class of the UITableView.
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
NSInteger numberOfSections = [contactSectionTitles count];
NSMutableArray *indexTitles = [NSMutableArray arrayWithCapacity: 0];
//fill the array with the titles you want
return indexTitles;
}
- (NSInteger) tableView: (UITableView *) tableView sectionForSectionIndexTitle: (NSString *) title atIndex: (NSInteger) index {
/*this method will get called when you click on a shortcut and it will scroll the UITableView to the respective section. But in our UITableView we have only one section, so when 'index' is not equal to 0 we'll have write some code to scroll the UITableView.*/
if (index != 0) {
// i is the index of the cell you want to scroll to
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:TRUE];
}
return index;
}
1 comment:
awesome post, I returning 0 from delegate instead of actual index value and it didn't work. Your code works great.
Post a Comment