问题
Currently I have a NSMutableArray that loads from NSUserDefaults in my app and feeds the data to my UITableView. The NSMutableArray consists of NSDictionarys which consist of 5 or 6 keys each.
Then in the view where the UITableView is, I have a UISegmentedControl where the user can then sort the UITableView based upon Date or some other things.
So when they click the date segment, I execute this code:
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"Date" ascending:NO];
[self.cellArray sortUsingDescriptors:[NSArray arrayWithObject:descriptor]];
The sorting itself works as I can see self.cellArray being sorted through NSLogs. Although when I call my tableview to reloadData, none of the NSDictionarys actually re-sort in the tableview itself. So programmatically they are being sorted but not in the UI which is extremely odd.
If use this for statement right after the code I posted above:
for (NSDictionary *dict in self.cellArray) {
NSLog(@"DateKey: %@", [dict objectForKey:@"Date"]);
}
This is what my NSLogs look like if I do ascending: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
If I do ascending:NO this is what my NSLogs look like:
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
So as you can see, the sorting indeed works properly but once I do reloadData after that code, my UITableView does not re-order the cells based upon the Date key.
My tableview is in fact connected in IB and is not nil. I do have a setter and getter for it to if that matters.
Does anyone have any idea why this happening? Or do you need more context/code?
Thanks!
回答1:
I'd be inclined to put your logging statement ...
for (NSDictionary *dict in self.cellArray)
NSLog(@"DateKey: %@", [dict objectForKey:@"Date"]);
in, right after you invoke sortUsingDescriptors as well in your cellForRowAtIndexPath method. I'd also put a NSLog statement right where cellArray is getting initialized (to make sure that you're not accidentally invoking that again ... easy to do if your routines get complicated).
I have to believe that the order in cellForRowAtIndexPath will not reflect the order you got immediately after your sort. Assuming this is the case, either cellArray is getting reset somewhere (i.e. where do you populate it? in viewDidLoad or somewhere else?), or your sortUsingDescriptors is not getting invoked like you think it is.
Just start putting break points and/or debugging messages in your various routines. It's got to be something simple. I just did a test of reloading where I resort as I reload, and the sort orders are being preserved. So it's got to be something simple.
来源:https://stackoverflow.com/questions/10675792/reloaddata-not-working-for-uitableview-datasource