与Three20的字母表TableView

时间:2011-12-14 08:53:50

标签: ios three20 uitableview

我有一个简单的TTTableViewController来代表一组公司。我想使用TableView右侧的部分和字母选择器按字母顺序对TableView进行排序。

使用Three20有一种简单的方法吗?

目前我没有任何单独的数据源。

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

 }

1 个答案:

答案 0 :(得分:4)

对于初学者,你应该使用TTSectionedDataSource。它通过具有2个NSMutableArray来支持部分 - 一个用于由字符串数组表示的部分,另一个用数组数组,每个部分包含表的项目。

获取字母也很简单。 UITableViewDataSource支持:

- (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView;

并且three20中的基类支持通过这样做来提取它们:

- (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView {
   return [TTTableViewDataSource lettersForSectionsWithSearch:YES summary:NO];
}

最好的解决方案是创建一个新的DataSource并从TTSectionedDataSource继承它,然后实现类似的东西来构建部分:       self.items = [NSMutableArray array];       self.sections = [NSMutableArray array];

  NSMutableDictionary* groups = [NSMutableDictionary dictionary];
  for (NSString* name in _addressBook.names) {
    NSString* letter = [NSString stringWithFormat:@"%C", [name characterAtIndex:0]];
    NSMutableArray* section = [groups objectForKey:letter];
    if (!section) {
      section = [NSMutableArray array];
      [groups setObject:section forKey:letter];
    }

    TTTableItem* item = [TTTableTextItem itemWithText:name URL:nil];
    [section addObject:item];
  }

  NSArray* letters = [groups.allKeys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
  for (NSString* letter in letters) {
    NSArray* items = [groups objectForKey:letter];
    [_sections addObject:letter];
    [_items addObject:items];
  }

有关完整的工作解决方案,请参阅three20源代码下的TTCatalog示例,在那里您将找到具有此代码的MockDataSource.m。

相关问题