Objective-C iPhone -在UITableView数据源的多个部分中排序数据

时间:2022-09-02 10:46:05

For the purpose of asking this question about ordering. The following MyObject class returns an instance with random generated category names.

为了问这个关于排序的问题。下面的MyObject类返回一个具有随机生成的类别名称的实例。

I use the following dataSource methods:

我使用以下数据源方法:

numberOfSections accessed with [dataSource count].

使用[dataSource count]访问的numberOfSections。

titleForSection accessed with [[dataSource objectAtIndex:indexPath.section] valueForKey:@"categoryName"].

使用[[[dataSource objectAtIndex:indexPath]访问titleForSection。部分]valueForKey:@“categoryName”)。

numberOfRowsInSection accessed with [[[dataSource objectAtIndex:indexPath.section] valueForKey:@"myObjects"] count].

使用[[[[dataSource objectAtIndex:indexPath]访问的numberOfRowsInSection。部分]valueForKey:@“myobject”)计数)。

And finally, the MyObject for each row is accessed with [[[dataSource objectAtIndex:indexPath.section] valueForKey:@"myObjects"] objectAtIndex:indexPath.row] on the cellForRowAtIndexPath method.

最后,使用[[[[dataSource objectAtIndex:indexPath]访问每一行的MyObject。部分]valueForKey:@“myobject”],objectAtIndex:indexPath。在cellForRowAtIndexPath方法上的行]。

I use the following code to create a dataSource that displays 9 section categories, however I'm a little stuck on the ordering of these categories and the data within. Assume there's an NSDate property as part of the MyObject class.

我使用下面的代码创建一个数据源,该数据源显示9个部分类别,但是我对这些类别和其中的数据的顺序有点迷惑。假设有一个NSDate属性作为MyObject类的一部分。

Question: How would I go about using this to display the records 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);
}

1 个解决方案

#1


2  

Easiest would be to sort your arrays, using NSMutableArray's sorting mutators or NSArray's sorting methods. Otherwise you'd have to construct some sort of mapping from input indices to dataSource indices for use by the various data source methods.

最简单的方法是使用NSMutableArray的排序变异体或NSArray的排序方法对数组进行排序。否则,您必须构造某种从输入索引到数据源索引的映射,以便各种数据源方法使用。


Edit Requested sample code for sorting, something like this should work. I assume you are wanting to sort everything by a property named date on the MyObject.

编辑所需的示例代码进行排序,类似这样的操作应该可以工作。我假设您希望对MyObject上的一个名为date的属性进行排序。

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

#1


2  

Easiest would be to sort your arrays, using NSMutableArray's sorting mutators or NSArray's sorting methods. Otherwise you'd have to construct some sort of mapping from input indices to dataSource indices for use by the various data source methods.

最简单的方法是使用NSMutableArray的排序变异体或NSArray的排序方法对数组进行排序。否则,您必须构造某种从输入索引到数据源索引的映射,以便各种数据源方法使用。


Edit Requested sample code for sorting, something like this should work. I assume you are wanting to sort everything by a property named date on the MyObject.

编辑所需的示例代码进行排序,类似这样的操作应该可以工作。我假设您希望对MyObject上的一个名为date的属性进行排序。

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