UITableView设置

时间:2013-11-08 21:03:16

标签: ios uitableview

我有一个未分类的NSArray名字。我知道如何构建一个静态表,但我不知道如何制作一个动态表,其中包含一个带有名字第一个字母的部分,然后将该字母的名称放入每个部分。我知道这听起来像初学者级别的编程问题,而且确实如此。任何帮助都会很棒。 iOS编程新手,所以我只想尝试一切。

1 个答案:

答案 0 :(得分:1)

您可以使用基于块的数据模型初始值设定项TLIndexPathTools轻松完成此操作:

NSArray *items = ...; // and array containing your unorganized data (items of type NSString assumed here)
TLIndexPathDataModel *dataModel = [[TLIndexPathDataModel alloc] initWithItems:items sectionNameBlock:^NSString *(id item) {
    return [[((NSString *)item) substringToIndex:1] upperCaseString];
} identifierBlock:nil];

sectionNameBlock参数是一个块,它返回给定项的第一个字母,初始化程序使用该块将数据组织成各个部分。然后,您在数据源中使用dataModel(而不是数组),并使用各种AP​​I(如[dataModel numberRowsInSection:][dataModel itemAtIndexPath:][dataModel indexPathForItem:]等)委派方法。

尝试运行"Blocks" sample project作为工作示例。