在TableView中使用组中的第一个字母自定义索引部分

时间:2014-02-08 01:25:40

标签: ios

我想自定义Index Section看起来像这样

enter image description here

这是我从Snap-Hack app获得的截图。

此索引部分只显示列表中的第一个联系人字母而非A-Z索引。

所以我有两个问题: 1.如何抓住第一个字母部分 2.在索引部分

中使用它

注意:朋友列表来自JSOn Data而非数据库

1 个答案:

答案 0 :(得分:2)

您可以使用TLIndexPathTools基于数据块的数据模型初始化程序,并使用几行代码执行此操作。自述文件本身显示了如何创建“第一个字母”部分:

// multiple sections defined by an arbitrary code block
TLIndexPathDataModel *dataModel = [[TLIndexPathDataModel alloc] initWithItems:items sectionNameBlock:^NSString *(id item) {
    // organize items by first letter of description (like contacts app)
    return [item.description substringToIndex:1];
} identifierBlock:nil];

该块只需返回给定项目的部分名称,您可以替换所需的任何逻辑。

<强>更新

包含节的表的典型数据模型是NSArray节的名称和NSDictionary的{​​{1}},其中包含每个节的行。 TLIndexPathDataModel取代了所有这些内容,并提供了NSArrays[dataModel numberOfRowsInSection:]等API,以简化数据模型和委托方法。例如,要实现节标题和索引,您可以执行以下操作:

[dataModel itemAtIndexPath:]

要查看此操作,请查看"Blocks" sample project。此项目使用了几个其他类- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [self.dataModel sectionNameForSection:section]; } - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return [self.dataModel sectionNames]; } - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { return [self.dataModel sectionForSectionName:title]; } TLTableViewController,您可以了解in the README。但如果需要,您可以单独使用TLIndexPathController