【问题标题】:How can I set a TableView's titleForHeaderInSection and numberOfRowsInSection with an NSDictionary of NSArrays?如何使用 NSArrays 的 NSDictionary 设置 TableView 的 titleForHeaderInSection 和 numberOfRowsInSection?
【发布时间】:2014-04-25 02:26:16
【问题描述】:

我有一个名称为 NSArrays 的 NSDictionary,所以自然总共有 26 个数组。我想用数组的键设置titleForHeaderInSection(A、B、C 等),用数组的计数设置numberOfRowsInSection。我可以通过使用块枚举它们来获取每个数组的计数,但是如何根据我拥有的 NSDictionary 在我的 TableView 上设置这两个选项?有什么方法可以对 NSDictionary 进行排序,以使结果按字母顺序排列?

这是我的 NSDictionary:

@{
    @"A" :  
        @[@"Person One", @"Person Two", @"Person Three"],
    @"B" :
        @[@"Person One", @"Person Two", @"Person Three", @"Person Four"],
    @"C" :
        @[@"Person One", @"Person Two"],
}
// etc

这是 NSDictionary 的 NSLog:标题会无序吗?

A -- 23
B -- 78
G => 27
U => 1
H => 57
V => 6
I => 1
// etc

【问题讨论】:

    标签: ios objective-c uitableview nsarray nsdictionary


    【解决方案1】:

    字典中的条目没有排序。你可以做的是创建一个数组 所有章节标题并按字母顺序排序:

    NSDictionary *dict = ...; // your dictionary
    NSArray *sectionTitles = [[dict allKeys] sortedArrayUsingSelector:@selector(compare:)];
    

    那么节数是

    [sectionTitles count]
    

    对于给定的节号,标题和行数可以计算为

    NSString *title = sectionTitles[section];
    NSUInteger numRows = [dict[sectionTitles[section]] count];
    

    最后,对于给定的索引路径,对应的项是

    NSString *item = dict[sectionTitles[indexPath.section]][indexPath.row];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多