多個部分詢問有關訂貨這個問題的目的範圍內訂貨數據。以下MyObject
類將使用隨機生成的類別名稱返回一個實例。Objective-C的iPhone - 的一個UITableView數據源
我用下面的方法dataSource
:
numberOfSections
與[dataSource count]
訪問。
titleForSection
與[[dataSource objectAtIndex:indexPath.section] valueForKey:@"categoryName"]
訪問。
numberOfRowsInSection
與[[[dataSource objectAtIndex:indexPath.section] valueForKey:@"myObjects"] count]
訪問。
最後,每一行的MyObject
與[[[dataSource objectAtIndex:indexPath.section] valueForKey:@"myObjects"] objectAtIndex:indexPath.row]
在cellForRowAtIndexPath
方法來訪問。
我使用下面的代碼來創建一個顯示9個節類別的dataSource
,但是我有點卡住了這些類別和其中的數據的排序。假設有NSDate
屬性作爲MyObject
類的一部分。
問題:我該如何去使用它以降序顯示記錄?
- (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);
}
我很好排序我的陣列。這是與這些數組一起排序的問題。 – fuzz 2011-03-15 02:45:51
如果您對每個行數組進行排序,則可以根據每個節的行數組中的第一個對象對節進行排序。或者,如果更適合您的數據集,則可以按照完全不同的方式對各個部分進行排序。 – Anomie 2011-03-15 02:53:30
@Anomie你介意提供一些代碼來做到這一點嗎? – fuzz 2011-03-16 12:43:57