views:

183

answers:

1

In iPhone native Phone book - there is a search character at the top & # character at the bottom.

I want to add both of that character in my table Index.

Currently I have implemented following code.

atoz=[[NSMutableArray alloc] init];

    for(int i=0;i<26;i++){
        [atoz addObject:[NSString stringWithFormat:@"%c",i+65]];
    }


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return atoz;
}

How to have # character & search symbol in my UITableView?

Thanks in advance for sharing your knowledge.

Sagar

+2  A: 

The best way to tackle this is to make use of the tools the framework provides. In this case, you want to use UILocalizedIndexedCollation (developer link).

I also have a decorator for this class that is designed to insert the {{search}} icon for you and handle the offsets. It is a like-for-like drop-in replacement for UILocalizedIndexedCollation.

I've posted a more in-depth description of how to use this on my blog. The decorator is available here (Gist).

The basic idea is to group your collection into an array of arrays, with each array representing a section. You can use UILocalizedIndexedCollation (or my replacement) to do this. Here's a small NSArray category method I use to do this:

@implementation NSArray (Indexing)

- (NSArray *)indexUsingCollation:(UILocalizedIndexedCollation *)collation withSelector:(SEL)selector;
{
    NSMutableArray *indexedCollection;

    NSInteger index, sectionTitlesCount = [[collation sectionTitles] count];  
    indexedCollection = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];

    for (index = 0; index < sectionTitlesCount; index++) {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [indexedCollection addObject:array];
        [array release];
    }

    // Segregate the data into the appropriate section
    for (id object in self) {
        NSInteger sectionNumber = [collation sectionForObject:object collationStringSelector:selector];
        [[indexedCollection objectAtIndex:sectionNumber] addObject:object];
    }

    // Now that all the data's in place, each section array needs to be sorted.
    for (index = 0; index < sectionTitlesCount; index++) {
        NSMutableArray *arrayForSection = [indexedCollection objectAtIndex:index];

        NSArray *sortedArray = [collation sortedArrayFromArray:arrayForSection collationStringSelector:selector];
        [indexedCollection replaceObjectAtIndex:index withObject:sortedArray];
    } 
    NSArray *immutableCollection = [indexedCollection copy];
    [indexedCollection release];

    return [immutableCollection autorelease];
}

@end

So, given an array of objects, for example books that I want to divide into sections based on their name (the Book class has a name method), I would do this:

NSArray *books = [self getBooks]; // etc...
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSArray *indexedBooks = [books indexUsingCollation:collation withSelector:@selector(name)];
Luke Redpath
I just realized that the name of this category method could be improved; given that the name implies that it modifies the receiver rather than a new Array, a better name would probably be indexedArrayUsingCollation:withSelector:.I also welcome any improvements to the above category. I'm sure it could be more efficient.
Luke Redpath