将 NSarray 拆分为多个部分时出错 [英] Error spliting NSarray into sections

查看:49
本文介绍了将 NSarray 拆分为多个部分时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我一直在研究一个与我想要实现的非常匹配的示例,唯一的区别是在示例中他直接从他的数据库中调用他需要分割的数据等.我已经有一个排序的 NSArray.

Okay so I have been working through an example that closely matches what I am trying to achive, the sole difference being that in the example he is directly calling from his database the data he needs to be sectioned etc. Where as I already have a sorted NSArray.

这是我正在编写的教程 - iPhone开发:创建像屏幕这样的原生联系人

This is the tutorial I am working off - iPhone Development: Creating Native Contacts like screen

我创建了一个方法来捕获 NSArray 中的每个条目并将这些结果放入基于 alpha 的 NSDictionary(因此它们将是 A、B、C...等的 NSDictionary)

I have created a Method that is capturing each entry in the NSArray and putting these results into a alpha based NSDictionary (so their will be a NSDictionary for A,B,C... etc)

这是我的方法.

//method to sort array and split for use with uitableview Index
- (IBAction)startSortingTheArray:(NSMutableArray *)arrayData
{
    //Sort incoming array alphabetically
    //sortedArray = [arrayData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    [self setSortedArray:[arrayData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]];

    arrayOfCharacters = [[NSMutableArray alloc]init];
    objectsForCharacters = [[NSMutableDictionary alloc]init];

    for(char c='A';c<='Z';c++)
    {

        if([sortedArray count] >0)
        {
            [arrayOfCharacters addObject:[NSString stringWithFormat:@"%c",c]];
            [objectsForCharacters setObject:sortedArray forKey:[NSString stringWithFormat:@"%c",c]];
            NSLog(@"%@", objectsForCharacters);
        }
        [sortedArray release];


    //Reloads data in table
    [self.tableView reloadData];
    }
}

这是将每个值放入每个 alpha 部分,我希望有人可以帮助我制作它,以便如果数组中有一个值,则只建立 alpha 部分..然后只将这些值加载到每个部分中,不是每个部分.

This is putting every value into every alpha section, I am hoping someone can help me with making it so that only alpha sections are established if there is a value in the array for it.. then only loading those values into each section, not every section.

推荐答案

这段代码将做到这一点,并且比为每个字母过滤一次数组效率更高.

This piece of code will do just that and will be much more efficient than filtering the array once for each letter.

//Sort incoming array alphabetically so that each sub-array will also be sorted.
NSArray *sortedArray = [arrayData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

// Dictionary will hold our sub-arrays
NSMutableDictionary *arraysByLetter = [NSMutableDictionary dictionary];

// Iterate over all the values in our sorted array
for (NSString *value in sortedArray) {

    // Get the first letter and its associated array from the dictionary.
    // If the dictionary does not exist create one and associate it with the letter.
    NSString *firstLetter = [value substringWithRange:NSMakeRange(0, 1)];
    NSMutableArray *arrayForLetter = [arraysByLetter objectForKey:firstLetter];
    if (arrayForLetter == nil) {
        arrayForLetter = [NSMutableArray array];
        [arraysByLetter setObject:arrayForLetter forKey:firstLetter];
    }

    // Add the value to the array for this letter
    [arrayForLetter addObject:value];
}

// arraysByLetter will contain the result you expect
NSLog(@"Dictionary: %@", arraysByLetter);

请注意,arraysByLetter 是一个字典,其中每个存在于初始数据中的第一个字母"包含一个数组.

Note that arraysByLetter is a dictionary that contains one array per "first letter" that exists in your initial data.

--- 添加于 2011-09-23 ---

--- Added on 2011-09-23 ---

[sortedArray removeAllObjects];
NSArray *sortedKeys = [arraysByLetter.allKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

for (NSString *key in sortedKeys) {
    [sortedArray addObject:key];
    [sortedArray addObjectsFromArray: [arraysByLetter objectForKey:key]];
}

NSLog(@"Sorted Array: %@", sortedArray);

输出如下:

C,
Computer,
H,
Helene,
Hello,
J,
Jules,
W,
World

这篇关于将 NSarray 拆分为多个部分时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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