将NSarray拆分为多个部分时出错

时间:2011-09-19 04:47:33

标签: iphone ios nsarray nsdictionary

好吧所以我一直在研究一个与我想要实现的内容非常匹配的例子,唯一的区别是在他的数据库中他直接从他的数据库中调用他需要被分割的数据等。我在哪里有一个排序的NSArray。

这是我正在处理的教程 - iPhone Development: Creating Native Contacts like screen

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

这是我的方法。

//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部分..然后只将这些值加载到每个部分而不是每个部分。

2 个答案:

答案 0 :(得分:4)

这段代码就是这样做的,并且比为每个字母过滤一次数组效率要高得多。

//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是一个字典,每个“首字母”包含一个数组,存在于初始数据中。

---添加于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

答案 1 :(得分:0)

您似乎需要使用每个字母的谓词过滤sortedArray。像这样......

for(char c='A';c<='Z';c++) {
    NSPredicate *predicate =
    [NSPredicate predicateWithFormat:@"SELF beginswith[c] '%c'", c];
    NSArray *objectsBeginningWithCurrentLetter = [array filteredArrayUsingPredicate:predicate];

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


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