Error spliting NSarray into sections

萝らか妹 提交于 2019-12-13 22:15:42

问题


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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/7466569/error-spliting-nsarray-into-sections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!