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

标签 ios

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

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/

相关文章:

android - 将 Xamarin.Forms.Color 转换为平台特定颜色

ios - 如何在 iOS 中使用 Twitter API 1.1 获取 Token

ios - 快速验证失败时在 UITextField 中自动显示错误图标

ios - GLKit:为 Swift 格式化 Objective-C GLKit 参数

ios - 在 Swift 中获取网页 HTML 代码

ios - 图像大小与屏幕大小成比例

iphone - 将 iPhone 或 iPod 变成无线网络摄像头

ios - OCMockito 验证和 NSError 的编译错误**

javascript - 在 React-native 中按下按钮后双触发

iphone - 在 UIImagePickerController 之后直接调用 MFMailComposeViewController