fbm351
fbm351

Reputation: 225

UITableView Setup

I have an unsorted NSArray of names. I know how to build a static table, but I am not sure how to make a dynamic table that will have a section with the first letter of the name and then the names for that letter in put into each section. I know this sounds like a beginner level programming question, and well it is. Any help would be great. New to iOS programming so I am just trying to get a feel for everything.

Upvotes: 0

Views: 71

Answers (1)

Timothy Moose
Timothy Moose

Reputation: 9915

You can do this easily with TLIndexPathTools using the block-based data model initializer:

NSArray *items = ...; // and array containing your unorganized data (items of type NSString assumed here)
TLIndexPathDataModel *dataModel = [[TLIndexPathDataModel alloc] initWithItems:items sectionNameBlock:^NSString *(id item) {
    return [[((NSString *)item) substringToIndex:1] upperCaseString];
} identifierBlock:nil];

The sectionNameBlock argument is a block that returns the first letter of the given item, which the initializer uses to organize the data into sections. Then you'd use dataModel (instead of an array) in your data source and delegate methods using the various APIs like [dataModel numberRowsInSection:], [dataModel itemAtIndexPath:], [dataModel indexPathForItem:], etc.

Try running the "Blocks" sample project for a working example.

Upvotes: 1

Related Questions