【问题标题】:Turning an NSArray of custom objects into a alphabetically sectioned UITableView with an index?将自定义对象的 NSArray 转换为按字母顺序划分的带有索引的 UITableView?
【发布时间】:2010-10-25 20:51:27
【问题描述】:

(嗯..长标题)

如果我从自定义对象的 NSArray(每个对象都是一个人)开始,如下所示:

{
  surname:Allen   forename: Woody,
  surname:Baxter  forename: Stanley,
  surname:Clay    forename: Cassius
}

..等等..

可以看到数组已经是姓氏了。如何使用该数组创建一个带有节标题 A、B C 等的 UITableView,以及垂直运行的侧面的索引事物

                                                               A
                                                               B
                                                               C

编辑

我想我要问的是如何将我的对象数组变成一个节数组,其中包含姓氏的所有首字母,以及一个姓氏数组,它可能是这样的嵌套数组:

{ 
  letter: a
  {  surname:Allen   forename: Woody   }
  letter: b
  {  surname:Baxter  forename: Stanley }
  etc
} 

事实上,也许只是一个嵌套数组就可以了。

【问题讨论】:

    标签: iphone objective-c uitableview ios nsarray


    【解决方案1】:

    可能有上百万种方法可以做到这一点:)这是一种可能有效的方法(我相信它可以做得更漂亮,但因为没有人回答):

    NSMutableDictionary *aIndexDictionary = [[NSMutableDictionary alloc] init]; 
    NSMutableArray *currentArray;
    NSRange aRange = NSMakeRange(0, 1);
    NSString *firstLetter;
    for (Person *aPerson in personArray) {
      firstLetter = [aPerson.surname substringWithRange:aRange];
      if ([aIndexDictionary objectForKey:firstLetter] == nil) {
        currentArray = [NSMutableArray array];
        [aIndexDictionary setObject:currentArray forKey:firstLetter];
      }
      [currentArray addObject:aPerson];
    }
    

    未经测试...

    【讨论】:

    • 哇,那个编辑完全没用@P.J.Radadiya。
    【解决方案2】:

    Joseph Tura 的回答有很多问题,正如他所说,未经测试。以下是问题:

    1) [aPerson.name substringWithRange:aRange] 如果名称为 nil 或 @"" 会崩溃;

    2) 如果 firstLetter 已经在键列表中,则当前数组不会重置为指向属于键 firstLetter 的数组对象。因此,如果 currentArray 先前指向字典中键为“K”的数组,但当前 firstLetter 为“P”已作为键存在,则将添加名称(以该字母“P”开头)到以“k”开头的名称数组。那里有大问题。

    3) 由于大小写的不同,Jassie 被添加到不同的数组到 jassie(大写和小写 'k' 的处理方式不同)。

    因此,作为对 Joseph 答案的巨大改进,第 2 点至关重要,以下是为未来用户提供的更准确的实现:

    -(NSDictionary*)indexArrayByName:(NSArray*)people
    {
        NSMutableDictionary *anIndexedDict = [[NSMutableDictionary alloc] init];
        NSMutableArray *currentArray = nil;
        NSRange aRange = NSMakeRange(0, 1);
        NSString *firstLetter;
    
        for(Person *aPerson in people)
        {
            if(aPerson.name.length)
                firstLetter = [[aPerson.name substringWithRange:aRange] uppercaseString];
            else
                firstLetter = @"noName";//find own way to identify these
    
            if(![anIndexedDict.allKeys containsObject:firstLetter])
            {
                currentArray = [NSMutableArray array];
                anIndexedDict[firstLetter] = currentArray;
            }
            currentArray = anIndexedDict[firstLetter];
            [currentArray addObject:aPerson];
        }    
        return anIndexedDict;
    }
    

    可以添加其他改进,例如删除前导空格等,但我只是想我会提到主要问题。我测试了约瑟夫对这些问​​题的回答,然后我的以确保它们被消除:)。感谢约瑟夫的主要思想。干杯。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      相关资源
      最近更新 更多