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