2011-12-14 5 views
1

私は、一連の企業を表す簡単なTTTableViewControllerを持っています。 TableViewの右側のセクションと文字セレクタを使って、TableViewをアルファベット順にソートしたいと思います。Alphabetic TableView with Three20

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

} 

答えて

4

まずはTTSectionedDataSourceを使用してください。これは、2つのNSMutableArrayを持つことでセクションをサポートします - 1つは文字列の配列で表されるセクション用で、もう1つは各セクション用のテーブル項目を持つ配列の配列です。

手紙を取得することもかなり簡単です。 UITableViewDataSourceはサポートされています。

three20で

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

と基底クラスがこれを行うことによって、それらを抽出サポートしています。

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

あなたのための最善の解決策は、新しいデータソースを作成し、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があります。