I want to replicate Contacts.app's scrollbar behavior.
I know that you must implement those two methods :
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;
And that what I've done : the first one returns an array of characters for A to Z, and the second one returns the index.
So far, it works quite well : I got the scrollbar, and scrolling on it makes the UITableView scroll.
But I have two questions :
First,the problem is, if I don't have contacts for the letter C for example, then the scrolling will obviously be staggered, and scrolling on C will show the D index, scrolling on D will show the E index...
I though about searching the closest section, but is that the most efficient way to do it ?
Something like implementing this function in my class :
- (NSInteger) getSectionIndexForSectionTitle:(NSString *section) {
if ([_allSections objectForKey:section] == nil) {
NSComparisonResult res = NSOrderedAscending;
for (NSString* sectionTitle in _allSections) {
NSComparisonResult tmp = [sectionTitle compare:section];
if (tmp == NSOrderedDescending && res == NSOrderedAscending)
return [(NSNumber *)[_allSections objectForKey:sectionTitle] intValue];
res = tmp;
}
return [_allSections count] - 1;
}
return [(NSNumber *)[_allSections objectForKey:section] intValue];
}
But I'm afraid of the poor performances that this solutions might provide.
Second question, that might be linked with the first one : In the Contacts.app, you can't see the "O" letter in the scrollbar, but you can still scroll to this section.
Any idea on how to achieve that ?
Thanks for reading and helping me !