Frage

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 !

War es hilfreich?

Lösung 2

To achieve both your problem I think there is an alternative solution for this.. Try to change the heightForHeaderInSection to hide the section header which make empty section to be hide so when scroll to that section index it will be shown closest section.

And you keep the all section title in array that returns from sectionIndexTitlesForTableView then user can scroll to that position.

So implement heightForHeaderInSection to hide the section that has no rows, and in your sectionIndexTitlesForTableView, sectionForSectionIndexTitle will just return whatever you have and don't try to manipulate index.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
    NSInteger nRowCount = [[_arrSectionCounts objectAtIndex:section] integerValue];
    if ( nRowCount == 0 ) {
        return 0.0f;
    }

    return 22.0f;
}

Andere Tipps

You don't need to hack section header heights. There's a class designed for exactly the purpose you're looking for: UILocalizedIndexedCollation. And there's a great writeup on using it to get index scrolling in a table view.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top