我想自定义索引部分看起来像这样
这是我从 Snap-Hack 应用程序中获得的屏幕截图。
此索引部分仅显示列表中的第一个联系人字母而不是 A-Z 索引。
所以我有两个问题: 1.如何抓取首字母段 2.在索引部分使用它
注意:好友列表是从 JSOn 数据中获取的,不是数据库中的
最佳答案
您可以使用 TLIndexPathTools基于 block 的数据模型初始化器,并用几行代码来完成。自述文件本身展示了如何创建“首字母”部分:
// 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];
该 block 只需要返回给定项目的部分名称,您可以替换您需要的任何逻辑。
更新:
具有部分的表的典型数据模型是部分名称的 NSArray
和包含每个部分的行的 NSArrays
的 NSDictionary
. TLIndexPathDataModel
替换所有这些并提供像 [dataModel numberOfRowsInSection:]
和 [dataModel itemAtIndexPath:]
这样的 API 来简化您的数据模型和委托(delegate)方法。例如,要实现章节标题和索引,您可以这样做:
- (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];
}
要查看实际效果,请查看 "Blocks" sample project .该项目使用了几个额外的类 TLTableViewController
和 TLIndexPathController
,您可以了解 in the README .但如果需要,您可以单独使用 TLIndexPathDataModel
。
关于ios - 使用 TableView 中组的第一个字母自定义索引部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21640693/