目前我有一个 NSMutableArray,它从我的应用程序中的 NSUserDefaults 加载并将数据提供给我的 UITableView。NSMutableArray 由 NSDictionary 组成,每个包含 5 或 6 个键。
然后在 UITableView 所在的视图中,我有一个 UISegmentedControl,用户可以在其中根据 Date 或其他一些东西对 UITableView 进行排序。
因此,当他们单击日期段时,我执行以下代码:
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"Date" ascending:NO];
[self.cellArray sortUsingDescriptors:[NSArray arrayWithObject:descriptor]];
排序本身起作用,因为我可以看到 self.cellArray 正在通过 NSLogs 进行排序。虽然当我调用我的 tableview 来重新加载数据时,没有一个 NSDictionarys 实际上在 tableview 本身中重新排序。因此以编程方式对它们进行排序,但不是在非常奇怪的 UI 中。
如果在我上面发布的代码之后使用这个 for 语句:
for (NSDictionary *dict in self.cellArray) {
NSLog(@"DateKey: %@", [dict objectForKey:@"Date"]);
}
如果我进行升序,这就是我的 NSLogs 的样子:YES:
2012-05-20 13:48:21.008 App[71308:707] DateKey: 2012-05-13 18:19:22 +0000
2012-05-20 13:48:21.009 App[71308:707] DateKey: 2012-05-13 18:19:29 +0000
2012-05-20 13:48:21.009 App[71308:707] DateKey: 2012-05-15 01:54:13 +0000
2012-05-20 13:48:21.010 App[71308:707] DateKey: 2012-05-15 04:01:15 +0000
2012-05-20 13:48:21.011 App[71308:707] DateKey: 2012-05-16 04:08:20 +0000
2012-05-20 13:48:21.012 App[71308:707] DateKey: 2012-05-16 04:13:59 +0000
2012-05-20 13:48:21.012 App[71308:707] DateKey: 2012-05-16 04:32:29 +0000
2012-05-20 13:48:21.013 App[71308:707] DateKey: 2012-05-16 04:32:38 +0000
如果我升序:不,这就是我的 NSLogs 的样子:
2012-05-20 13:56:25.956 App[71468:707] DateKey: 2012-05-16 04:32:38 +0000
2012-05-20 13:56:25.960 App[71468:707] DateKey: 2012-05-16 04:32:29 +0000
2012-05-20 13:56:25.961 App[71468:707] DateKey: 2012-05-16 04:13:59 +0000
2012-05-20 13:56:25.961 App[71468:707] DateKey: 2012-05-16 04:08:20 +0000
2012-05-20 13:56:25.962 App[71468:707] DateKey: 2012-05-15 04:01:15 +0000
2012-05-20 13:56:25.962 App[71468:707] DateKey: 2012-05-15 01:54:13 +0000
2012-05-20 13:56:25.963 App[71468:707] DateKey: 2012-05-13 18:19:29 +0000
2012-05-20 13:56:25.964 App[71468:707] DateKey: 2012-05-13 18:19:22 +0000
如您所见,排序确实可以正常工作,但是一旦我在该代码之后执行 reloadData,我的 UITableView 就不会根据 Date 键对单元格重新排序。
我的 tableview 实际上是在 IB 中连接的,并且不是 nil。如果这很重要,我确实有一个 setter 和 getter。
有谁知道为什么会这样?还是您需要更多上下文/代码?
谢谢!