【问题标题】:Sort UITableView descending order对 UITableView 进行降序排序
【发布时间】:2014-04-22 20:03:52
【问题描述】:
- (NSArray *)combinedStrings {
return [[self.numberOfUsers arrayByAddingObjectsFromArray:self.numberOfModerators]    arrayByAddingObjectsFromArray:self.numberOfAdmins];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
  NSDictionary *jsonForJam = [self.combinedStrings objectAtIndex:indexPath.row];
  return cell;
  }

我想对tableView中显示的结果进行降序排序,怎么办?

我已经尝试过使用它,但没有帮助:Best way to sort an NSArray of NSDictionary objects?

基于我提供的示例代码的解决方案将非常有帮助。

【问题讨论】:

  • 您已就该主题提出的前两个问题的答案有什么问题?
  • @rmaddy 这对我有用,但我也被困在这部分。
  • “这是一个表格视图”是什么意思?您不是在尝试对 UITableView 进行排序,而是在尝试对 NSArray 进行排序。重复的大卫指出你做同样的事情:排序一个 NSArray。

标签: ios objective-c uitableview sorting


【解决方案1】:

您可以使用以下代码对字符串数组进行排序。

self.combinedStrings = [[[self.combinedStrings sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] reverseObjectEnumerator] allObjects];

NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO selector:@selector(localizedCompare:)];
self.combinedStrings = [self.combinedStrings sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

【讨论】:

  • 它对我不起作用,也许我没有以正确的方式实现它。能否请您准确一点。基于上面我自己的代码的示例代码将非常有帮助。
  • 我猜“问题”是combindedStrings 不是属性,所以代码不会为他/她编译。 OP 可能应该阅读基本(Objective-C)编程。
【解决方案2】:

A)您可以实现自己的自定义排序方法(最终仍然使用 iOS 排序方法),因为 NSDictionary 使事情变得复杂。

B) 创建一个对象(其目的是保存字典中的所有信息)然后将自定义对象存储在数组中,然后像这样或按您希望的任何方式对其进行排序:

self.combinedStrings = [self.combinedStrings sortedArrayUsingComparator:^(id obj1, id obj2)
                    {
                        Client *client1 = (Client*)obj1;
                        Client *client2 = (Client*)obj2;
                        if([client1.firstName isEqualToString:client2.firstName])
                        return [client1.lastName compare:client2.lastName];
                        else
                        return [client1.firstName compare:client2.firstName];
                    }];

这里的“客户端”是自定义对象,数据存储在属性中以便于访问。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 2016-10-01
    • 2014-02-16
    • 2012-02-19
    相关资源
    最近更新 更多