views:

40

answers:

2

Hey. I'm a newbie in iPhone development. I'm working on project where I have to generate sections alphabetically in tableview. I dont want to show those sections which don't have any rows. My row values are generated at runtime and are not static so at least show me a code snippet which will help me....

A: 

When looping through to add the rows, kep track of the current section. When the current section is different than the section in the row, add a new section before adding the row.

Jackson Miller
+1  A: 

You can't hide sections using the API. Instead, you'll have to create an array of those sections that have items. Something like this:

NSArray *items = ...;
NSMutableArray *sectionHeaders = [[NSMutableArray alloc] initWithCapacity:100];
unichar currentChar = 0, lastChar = 0;

for (NSString *item in items) {
    currentChar = [item characterAtIndex:0];

    if (currentChar != lastChar) {
        lastChar = currentChar;
        [sectionHeaders addObject:[NSString initWithCharacters:&currentChar length:1]];
    }
}
Frank Schmitt