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

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

最佳答案

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

//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

关于iphone - 将 NSarray 拆分为多个部分时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7466569/

相关文章:

ios - 是否可以强制设备的日期测试本地通知?

android - ios相当于android xml的是什么?

ios - 在应用程序之间跟踪用户的方式?

objective-c - 保存 MKOverlayView 数组 -> plist

ios - UIImage 在 UIImageView iOS 中打开/关闭

iphone - 在主 `UITableViewCell` 已经加载后,如何用 `UIScrollView` 或 `UITableView` 替换 `UITableView`?

ios - 如何将一个NSArray数据添加到另一个NSArray字段?

iphone - 将 TableView 转换为具有部分

iphone - iOS 开发 : Can I store an array of integers in a Core Data object without creating a new table to represent the array?

iphone - xcode 4.2 快捷键