I have a simple TTTableViewController to represent a set of companies. I would like to sort the TableView alphabetically using sections and the letter selector on the right side of the TableView.
Is there an easy way to do this using Three20?
Currently I don’t have any separate datasource.
- (void)createModel {
NSMutableArray* itemsArray = [[NSMutableArray alloc] init];
for(IDCompany* company in companies) {
[itemsArray addObject:[TTTableSubtitleItem itemWithText:[company title] subtitle:[company companyDescription] URL:[company urlToDetailView]]];
}
self.dataSource = [TTListDataSource dataSourceWithItems:itemsArray];
[itemsArray release];
}
for starters you should use TTSectionedDataSource. It supports sections by having 2 NSMutableArray – one for sections represented by an array of strings and the other by array of arrays with the items of the table for each section.
Getting the letters is pretty simple too. UITableViewDataSource supports:
- (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView;and the base class in three20 supports extracting them by doing this:
The best solution for you would be to create a new DataSource and inherit it from TTSectionedDataSource, then implement something like this to build the sections:
self.items = [NSMutableArray array];
self.sections = [NSMutableArray array];
For a complete working solution refer to the TTCatalog samples under the three20 source and in there you will find MockDataSource.m that has this code.