Iphone 性能问题为UITableView创建节索引标题

Iphone 性能问题为UITableView创建节索引标题,iphone,ios,uitableview,Iphone,Ios,Uitableview,我在一个表视图中显示了一组联系人([[ContactStore sharedStore]allContacts]),并将列表划分为字母部分。我使用以下代码返回联系人的第一个字母数组,以及每个字母的条目数字典 //create an array of the first letters of the names in the sharedStore nameIndex = [[NSMutableArray alloc] init]; //create a dictionary to sa

我在一个表视图中显示了一组联系人([[ContactStore sharedStore]allContacts]),并将列表划分为字母部分。我使用以下代码返回联系人的第一个字母数组,以及每个字母的条目数字典

    //create an array of the first letters of the names in the sharedStore
nameIndex = [[NSMutableArray alloc] init];

//create a dictionary to save the number of names for each first letter
nameIndexCount = [[NSMutableDictionary alloc]init];

for (int i=0; i<[[[ContactStore sharedStore]allContacts]count]; i++){

//Get the first letter and the name of each person
    Contact *p = [[[ContactStore sharedStore]allContacts]objectAtIndex:i];
    NSString *lastName = [p lastName];
    NSString *alphabet = [lastName substringToIndex:1];


    //If that letter is absent from the dictionary then add it and set its value as 1 
    if ([nameIndexCount objectForKey:alphabet] == nil) {
        [nameIndex addObject:alphabet];
        [nameIndexCount setValue:@"1" forKey:alphabet];
    //If its already present add one to its value
    } else {

        NSString *newValue = [NSString stringWithFormat:@"%d", ([[nameIndexCount valueForKey:alphabet] intValue] + 1)];

        [nameIndexCount setValue:newValue forKey:alphabet];
    }
} 
//创建sharedStore中姓名首字母的数组
nameIndex=[[NSMutableArray alloc]init];
//创建字典以保存每个第一个字母的名称数
nameIndexCount=[[NSMutableDictionary alloc]init];

对于(int i=0;i请考虑将联系人存储在核心数据中,并使用NSFetchedResultsController

NSFetchedResultsController将只加载在表视图上可见的行的子集,这样用户就不必等待对所有联系人进行排序

NSFetchedResultsController还将按属性(即名字或姓氏)对联系人进行排序,您可以将分区标题设置为要排序的字段的第一个字母


看看这个和这个。

尽管Bio-Cho有一个很好的观点,但是通过调用

[[ContactStore sharedStore]allContacts]
只有一次。例如:

nameIndex = [[NSMutableArray alloc] init];
nameIndexCount = [[NSMutableDictionary alloc] init];

/*
 Create our own copy of the contacts only once and reuse it
 */
NSArray* allContacts = [[ContactStore sharedStore] allContacts];

for (int i=0; i<[allContacts count]; i++){
    //Get the first letter and the name of each person
    Contact *p = allContacts[i];
    NSString *lastName = [p lastName];
    NSString *alphabet = [lastName substringToIndex:1];

    //If that letter is absent from the dictionary then add it and set its value as 1 
    if ([nameIndexCount objectForKey:alphabet] == nil) {
        [nameIndex addObject:alphabet];
        [nameIndexCount setValue:@"1" forKey:alphabet];
    //If its already present add one to its value
    } else {
        NSString *newValue = [NSString stringWithFormat:@"%d", ([[nameIndexCount 
            valueForKey:alphabet] intValue] + 1)];

        [nameIndexCount setValue:newValue forKey:alphabet];
    }
} 
nameIndex=[[NSMutableArray alloc]init];
nameIndexCount=[[NSMutableDictionary alloc]init];
/*
只创建一次我们自己的联系人副本并重复使用
*/
NSArray*所有联系人=[[ContactStore sharedStore]所有联系人];

对于(int i=0;iThanks Rickay为您的答案,它指出了问题所在,我进一步调查,发现延迟不是枚举,而是第一次访问共享存储。我现在让应用程序更早地访问共享存储,并且加载速度非常快。谢谢!没问题。您可能想在ApplicationId中调用-sharedStore在你的应用程序的委托中完成启动,以便联系人从非常好的地方开始加载。但是,如果应用程序用一个非常大的数据集测试的话,你需要考虑一下会发生什么。你不能太小心。祝你好运!