2013-07-29 143 views
-1

我想創建一個索引表。索引表中只有一個部分

-(void)awakeFromNib 
{ 
self.heroes = [NSArray arrayWithObjects: @"Aaaa",@"Abbbbb",@"Bbbbb",@"Nnnnn",@"Pppp",@"Xxxx",@"Zzzzz", nil]; 
} 



- (NSArray *)sortedArrayFromArray:(NSArray *)array collationStringSelector:(SEL)selector{ 
return [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex: section]; 
} 

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]; 
} 

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { 
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index]; 
} 

但是,當應用程序啓動時,只有一個名爲「A」的索引。我究竟做錯了什麼?

(陣列顯示正確)

謝謝!

+0

你有這個方法嗎? - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView – Sauvage

+0

這裏有詳細的例子http://www.iphonedevcentral.com/indexed-uitableview-tutorial/ – Sauvage

回答

0

你還沒有實現的一些方法似乎

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [_heroes count]/*do according to your need*/; 
} 
在這種情況下 numberOfRowsInSection

應該非常小心,實際上告訴你多少應該是排一節下實施(說「A」或「B」等)。

比它可能適合你。