-1

我正在尝试设置我的 TableView,但遇到了麻烦。我的 NSArray 的值彼此不同。我知道这很难理解,但我会尽力解释。例如

-(void)setUpContacts{
//set up the contacts
NSString *string = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";

letterArray = [[NSMutableDictionary alloc]init];

Contacts *contact = [[Contacts alloc]init];
contactNumbers = [contact phoneNumbers];

for (NSDictionary* info in contactNumbers) {
    firstLetter = [info objectForKey:@"lastName"];
    index = @"_";
    if([firstLetter length] > 0){
        firstLetter =[firstLetter substringToIndex:1];
        firstLetter= [firstLetter capitalizedString];
        NSRange range = [string rangeOfString:firstLetter];
        if(range.length >= 0){
            index= firstLetter;
        }
    }
    if(![letterArray objectForKey:index]){

        [letterArray setValue:[NSMutableArray array] forKey:index];
    }
    [[letterArray objectForKey:index] addObject:info];    
}NSLog(@"%@",letterArray);}

-(NSArray *)getLetterArraycount{
NSMutableArray *countArray = [[NSMutableArray alloc]init];
NSNumber *subCount;

for(id key_main in letterArray){
    subCount = 0;

    for(id key_sub in [letterArray objectForKey:key_main]){
        subCount = [NSNumber numberWithInt:[subCount intValue]+1];
    }
    [countArray addObject:subCount];

}
return countArray;

}

- (void)viewDidLoad{
[super viewDidLoad];
[self setUpContacts];
countArrays = [self getLetterArraycount];    
indexPaths = [[NSMutableDictionary alloc]init];   

NSArray *temp = [letterArray allKeys];
NSLog(@"%@",temp);

NSLog(@"%@",countArrays);

//set up the tableView
self.myTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
self.title = @"Select Friends";
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:myTableView];}

它在控制台中像这样记录

    C =     (
            {
        firstName = Alex;
        kind = Mobile;
        lastName = Chang;
        name = "Alex Chang (Mobile)";
        number = "(555) 555-5555";
    },
            {
        firstName = YuYu;
        kind = Mobile;
        lastName = Chen;
        name = "YuYu Chen (Mobile)";
        number = "(408) 112-2334";
    },
            {
        firstName = Chris;
        kind = Mobile;
        lastName = Choi;
        name = "Chris Choi (Mobile)";
        number = "(999) 999-9999";
    },
            {
        firstName = Kevin;
        kind = Mobile;
        lastName = Chung;
        name = "Kevin Chung (Mobile)";
        number = "1 (231) 241-2312";
    }
);
H =     (
            {
        firstName = Danny;
        kind = Mobile;
        lastName = Huang;
        name = "Danny Huang (Mobile)";
        number = "(408) 599-9770";
    },
            {
        firstName = Ice;
        kind = Mobile;
        lastName = Huang;
        name = "Ice Huang (Mobile)";
        number = "(408) 444-4444";
    }
);
K =     (
            {
        firstName = Will;
        kind = Mobile;
        lastName = King;
        name = "Will King (Mobile)";
        number = "(415) 123-4567";
    }
);
L =     (
            {
        firstName = "";
        kind = iPhone;
        lastName = LastName;
        name = " LastName (iPhone)";
        number = "(408) 123-2555";
    },
            {
        firstName = david;
        kind = Mobile;
        lastName = lub;
        name = "david lub (Mobile)";
        number = "(666) 666-1111";
    }
);
P =     (
            {
        firstName = Jennifer;
        kind = Mobile;
        lastName = Pie;
        name = "Jennifer Pie (Mobile)";
        number = "(666) 666-6666";
    }
);
S =     (
            {
        firstName = Martin;
        kind = Mobile;
        lastName = Shen;
        name = "Martin Shen (Mobile)";
        number = "(415) 333-1234";
    },
            {
        firstName = Francis;
        kind = Mobile;
        lastName = Shung;
        name = "Francis Shung (Mobile)";
        number = "(408) 345-6789";
    }
);
V =     (
            {
        firstName = Brian;
        kind = Mobile;
        lastName = Vu;
        name = "Brian Vu (Mobile)";
        number = "(408) 234-5678";
    }
);
Y =     (
            {
        firstName = Qiu;
        kind = Mobile;
        lastName = Yang;
        name = "Qiu Yang (Mobile)";
        number = "(408) 567-8901";
    }
);
Z =     (
            {
        firstName = Joy;
        kind = Mobile;
        lastName = Zhou;
        name = "Joy Zhou (Mobile)";
        number = "(408) 765-4321";
    }
);
"_" =     (
            {
        firstName = FirstName;
        kind = Mobile;
        lastName = "";
        name = "FirstName  (Mobile)";
        number = "1 (408) 234-5141";
    }
);

}

2012-04-26 16:14:05.195 UpOut[2693:15803] (
L,
Y,
V,
"_",
Z,
S,
K,
C,
P,
H

我的问题是为什么它不首先记录“C”,而是记录“L”

4

2 回答 2

3

按字母顺序排列的答案始终获得更多支持:

NSArray *sortedArray = [countArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
于 2012-04-26T23:49:03.740 回答
3

听起来你需要学习如何使用NSSortDescriptor(我已经为你链接了文档)。

这是一个相关的问题,可能有一些有用的示例代码,您可以参考。在您的情况下,您想要排序的键将是每个 NSDictionary 条目的 lastName 值。

于 2012-04-26T23:33:01.750 回答