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

发布于 2024-12-05 04:04:30 字数 1412 浏览 0 评论 0原文

好吧,我一直在研究一个与我想要实现的目标非常匹配的示例,唯一的区别是在该示例中,他直接从数据库中调用他需要分段的数据等。 NSArray。

这是我正在研究的教程 - iPhone 开发:创建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 部分。然后仅将这些值加载到每个部分,而不是每个部分部分。

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.

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

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)

here is my method.

//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];
    }
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铁轨上的流浪者 2024-12-12 04:04:30

这段代码就可以做到这一点,并且比为每个字母过滤一次数组要高效得多。

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

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);

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

--- 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);

The output is the following:

C,
Computer,
H,
Helene,
Hello,
J,
Jules,
W,
World
蹲墙角沉默 2024-12-12 04:04:30

看起来您需要使用每个字母的谓词来过滤 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];
}

Looks like you need to filter sortedArray with a predicate for each letter. Something like this...

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文