gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:16:05 32 4
gpt4 key购买 nike

我想自定义索引部分看起来像这样

enter image description here

这是我从 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 和包含每个部分的行的 NSArraysNSDictionary . 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 .该项目使用了几个额外的类 TLTableViewControllerTLIndexPathController,您可以了解 in the README .但如果需要,您可以单独使用 TLIndexPathDataModel

关于ios - 使用 TableView 中组的第一个字母自定义索引部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21640693/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com