【问题标题】:How do I make NSSortDescriptor for Array Work?如何使 NSSortDescriptor for Array 工作?
【发布时间】:2013-02-01 00:37:06
【问题描述】:

我设法创建了ArrayMyLocation 对象,它们的距离属性设置在tabBarController 的2 个选项卡中的第一个。在我的第二个标签中,我在viewDidLoad 中得到它:

[self setAnnotationsToSort:myDelegate.annotationsToSort];

在这一行之后我调用了这个方法:

-(void)sort{
    NSSortDescriptor *sortDescriptor;
    sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"distance" ascending:YES] autorelease];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSArray *sortedArray;
    sortedArray = [self.annotationsToSort sortedArrayUsingDescriptors:sortDescriptors];
    [self.tableView reloadData];
}

应该对我的Array 进行排序。但是我的tableView 当前加载了Arrayself.annotationsToSort 的未排序版本。如何重新加载 tableView 但强制它使用新的 sortedArray

【问题讨论】:

    标签: ios objective-c nsarray nssortdescriptor


    【解决方案1】:

    你没有对sortedArray 做任何事情。试试

    self.annotationsToSort = [self.annotationsToSort sortedArrayUsingDescriptors:sortDescriptors];
    

    【讨论】:

    • 是的,我试过了,但显然 sortedArrayUsingDescriptors 方法返回一个 NSArray 而我的 self.annotationsToSort 是 NSMutableArray
    • 只需将其设为mutableCopy,以便您可以将其存储为NSMutableArray
    【解决方案2】:

    呼叫:

    self.annotationsToSort = sortedArray;

    致电[self.tableView reloadData];之前

    【讨论】:

      【解决方案3】:

      我错过了这一步:

      self.annotationsToSort = [(NSArray*)sortedArray mutableCopy];
      

      谢谢大家

      【讨论】:

        猜你喜欢
        • 2016-02-04
        • 2016-06-25
        • 1970-01-01
        • 2019-01-01
        • 2011-02-07
        • 2012-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多