(Object-C)iOS编程,类似于通讯录的项目

从服务器取得N个字典,其中有联系人等信息,需要做一个UITableView,按联系人按首字母放入‘A’-‘Z’的section中,右侧还有索引。现在索引和section都已经完成,联系人首字母(大写)已经取到,就是卡在排序这里。
我的想法是将字典作为value,首字母做为key,存入一个可变字典内,可是新创建的key只有一个字典,再添加(setObject)就会把原value取代,而将该key已有的字典存入数组内,再添加新的字典程序就直接崩掉了....
求大神指导或者提供思路,有类似的demo更好,非常感谢~
最新回答
我不会写诗

2024-04-21 04:51:21

你既然已经把这个((联系人的字典)的数组)都已经拿到了    那么现在进行下面的步骤:

  1. 新建一个字典,并且给这个字典加入24个键值对,键值对的Key分别是 @"A",@"B",@"C"……@"Z",每一个键值对的Object都是一个 NSMutableArray 并且是初始化好了的NSMutableArray

  2. 遍历你之前得到的这个(联系人的字典)的数组,依据首字母,分别把联系人放到字典(1步骤)内的对应的数组上,

  3. 现在你得到了一个   具有24个Key的字典    并且这每一个Key对应的是一个数组,你之后在设置你的UITableView的时候比较方便

  4. 具体的方式就这样,有什么不明白的你可以QQ联系我,这儿我不是来的太频繁,429801517

下面是我花了一会写的个范例代码,你看看吧

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableDictionary *dic = [self BuildDictionary];
    NSLog(@"%@",dic);
}

-(NSMutableDictionary *)BuildDictionary
{//这个函数返回的就是我们最终需要的了
    NSMutableArray * arr = [self BuildPeopleCard];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    char a = 'A';
    do{
        [dic setObject:[[NSMutableArray alloc] init] forKey:[NSString stringWithFormat:@"%c",a]];
        a++;
    }while (a <= 'Z');
    for (NSString *str in arr)
    {
        char c = [str characterAtIndex:0];
        if (c >= 'a' && c<= 'z')
        {//对于你来说这儿也非必须
            c = c - 'a' + 'A';
        }
        NSString *LastStr = [NSString stringWithFormat:@"%c%@",c,[str substringFromIndex:1]];
        [dic[[NSString stringWithFormat:@"%c",c]] addObject:LastStr];
    }
    return dic;
}

-(NSMutableArray *)BuildPeopleCard
{//这个函数只是用于生成一个数组而已,对于你来说这个函数是不必的,因为你已经从服务器拿到了这个数组了
    NSMutableArray *PeopleCard = [[NSMutableArray alloc]init];
    NSMutableString *str = [NSMutableString stringWithString:@"Occasionally, Dad would get out his mandolin and play for the family. We three children: Trisha, Monte and I, George Jr., would often sing along. Songs such as the Tennessee Waltz, Harbor Lights and around Christmas time, the well-known rendition of Silver Bells. Silver Bells, Silver Bells, its Christmas time in the city would ring throughout the house. One of Dad's favorite hymns was The Old Rugged Cross. We learned the words to the hymn when we were very young, and would sing it with Dad when he would play and sing. Another song that was often shared in our house was a song that accompanied the Walt Disney series: Davey Crockett. Dad only had to hear the song twice before he learned it well enough to play it. Davey, Davey Crockett, King of the Wild Frontier was a favorite song for the family. He knew we enjoyed the song and the program and would often get out the mandolin after the program was over. I could never get over how he could play the songs so well after only hearing them a few times. I loved to sing, but I never learned how to play the mandolin. This is something I regret to this day. "];
    while (str.length > 1)
    {
        [PeopleCard addObject:[str substringToIndex:[str rangeOfString:@" "].location]];
        [str deleteCharactersInRange:NSMakeRange(0, [str rangeOfString:@" "].location + 1)];
    }
    return PeopleCard;
}