美文网首页
iOS开发:UITableVIew实现类似于电话本的首字母索引

iOS开发:UITableVIew实现类似于电话本的首字母索引

作者: 静守幸福 | 来源:发表于2015-10-07 15:38 被阅读5491次

UITableVIew实现类似于电话本的首字母索引

实际上UITableView默认就支持象电话本那样的按首字母索引。 实现sectionIndexTitlesForTableView 和 sectionForSectionIndexTitle 这两个接口即可。 细节请参考UITableViewDataSource帮助文档。

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

{

NSMutableArray *toBeReturned = [[NSMutableArray alloc]init];

for(char c = ‘A’;c<=‘Z’;c++)

[toBeReturned addObject:[NSString stringWithFormat:@"%c",c]];

return toBeReturned;

}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index

{

NSInteger count = 0;

for(NSString *character in arrayOfCharacters)

{

if([character isEqualToString:title])

return count;

count ++;

}

return 0;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

if([arrayOfCharacters count]==0)

return @”";

return [arrayOfCharacters objectAtIndex:section];

}

相关文章

网友评论

      本文标题:iOS开发:UITableVIew实现类似于电话本的首字母索引

      本文链接:https://www.haomeiwen.com/subject/tscncttx.html