shivang
shivang

Reputation: 91

How to prevent the display of sections with no rows in UITableView?

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....

Upvotes: 1

Views: 626

Answers (2)

Frank Schmitt
Frank Schmitt

Reputation: 25785

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]];
    }
}

Upvotes: 1

Jackson Miller
Jackson Miller

Reputation: 1510

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.

Upvotes: 0

Related Questions