将自定义对象的NSArray转换为带有索引的按字母顺序排列的UITableView? [英] Turning an NSArray of custom objects into a alphabetically sectioned UITableView with an index?

查看:77
本文介绍了将自定义对象的NSArray转换为带有索引的按字母顺序排列的UITableView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(嗯......长标题)



如果我开始使用NSArray自定义对象(每个对象都是Person),就像这样:



'pre> {
姓:阿伦用名字:木香,
姓:巴克斯特用名字:斯坦利,
姓:粘土用的名字:Cassius
}

..等..



您可以看到该数组已经按姓氏顺序排列。如何使用该数组创建一个带有A,BC等节的UITableView,以及一个垂直运行的索引节点

  A 
B
C

编辑



我想我要问的是如何将我的对象数组转换成一个截面数组,包含Surnames的所有首字母和一个Surname数组这可能是这样的嵌套数组:

  {
letter:a
{姓:Allen forename :Woody}
letter:b
{姓:Baxter姓:Stanley}

}

事实上,也许只是一个嵌套数组。

解决方案

有可能百万种方式这样做:)在这里可能有用的(我相信它可以做得更漂亮,但是因为没有人回答):

  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];
}

未经测试......


(hmm.. long title)

If I start out with an NSArray of custom objects (each object is a Person) like this:

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

..etc..

You can see that the array is already in surname order. How do I use that array to create a a UITableView with a section headers A, B C etc, as well as an index thing on the side running vertically

                                                               A
                                                               B
                                                               C

EDIT

I guess what I'm asking is how to turn my array of objects into a Section Array, with all the first letters of the Surnames, and a Surname Array which is perhaps a nested array like this:

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

In fact, perhaps just a nested array will do.

解决方案

There are probably a million ways of doing this :) Here's one that might work (I am sure it can be done much prettier, but since no one is answering):

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];
}

Untested...

这篇关于将自定义对象的NSArray转换为带有索引的按字母顺序排列的UITableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆