Objective-C iPhone - Ordering data in multiple sections of a UITableView dataSource

To ask this question about the order. The following class MyObjectreturns an instance with names with an arbitrary generated category.

I use the following methods dataSource:

numberOfSectionsavailable from [dataSource count].

titleForSectionavailable from [[dataSource objectAtIndex:indexPath.section] valueForKey:@"categoryName"].

numberOfRowsInSectionavailable from [[[dataSource objectAtIndex:indexPath.section] valueForKey:@"myObjects"] count].

And finally, MyObjectfor each line, it accesses using [[[dataSource objectAtIndex:indexPath.section] valueForKey:@"myObjects"] objectAtIndex:indexPath.row]the method cellForRowAtIndexPath.

I use the following code to create dataSource, which displays 9 categories of sections, however I am a little fixated on organizing these categories and data internally. Suppose a property NSDateis part of a class MyObject.

Question: How can I use this to display entries in descending order?

- (void)createDatasource
{
    NSInteger numberOfObjects = 10;
    NSMutableArray *objects = [NSMutableArray arrayWithCapacity:numberOfObjects];
    NSMutableArray *categories = [NSMutableArray arrayWithCapacity:numberOfObjects];
    for (int i = 0; i < numberOfObjects; i++)
    {
        MyObject *obj = [[MyObject alloc] init];
        [objects addObject:obj];
        [categories addObject:obj.category];
        [obj release];
    }
    NSSet *set = [NSSet setWithArray:categories];
    NSMutableArray *dataSource = [[NSMutableArray alloc] initWithCapacity:[set count]];
    for (NSString *categoryString in set)
    {
        NSMutableDictionary *mainItem = [[NSMutableDictionary alloc] initWithObjectsAndKeys:nil, @"categoryName", nil, @"myObjects", nil];
        NSMutableArray *mainItemMyObjects = [NSMutableArray array];
        [mainItem setValue:categoryString forKey:@"categoryName"];
        for (MyObject *obj in objects)
        {
            if ([obj.category isEqualToString:categoryString])
            {
                [mainItemMyObjects addObject:obj];
            }
        }
        [mainItem setValue:mainItemMyObjects forKey:@"myObjects"];
        [dataSource addObject:mainItem];
        [mainItem release];
    }
    NSLog (@"objects = %@\ncategories = %@\nset = %@\ndatasource = %@", objects, categories, set, dataSource);
}
+3
1

, NSMutableArray NSArray. - dataSource .


, - . , date MyObject.

// First, sort the myObject mutable array in each category
for (NSDictionary *d in dataSource) {
    [[d valueForKey:@"myObjects"] sortUsingComparator:^NSComparisonResult(id o1, id o2){
        // Compare dates. NSDate 'compare:' would do ascending order, so if we just
        // reverse the order of comparison they'll come out descending.
        return [[o2 date] compare:[o1 date]];
    }];
}

// Second, sort the categories by the earliest dated object they contain
[dataSource sortUsingComparator:^NSComparisonResult(id o1, id o2){
    // Extract the first object from each category array, which must be the
    // earliest it contains due to the previous sort.
    MyObject *myObject1 = [[o1 valueForKey:@"myObjects"] objectAtIndex:0];
    MyObject *myObject2 = [[o2 valueForKey:@"myObjects"] objectAtIndex:0];

    // Compare dates, as above.
    return [[myObject2 date] compare:[myObject1 date]];
}];
+2

All Articles