问题
iOS 5 finally added a moveRowAtIndexPath:toIndexPath:
method to UITableView
. Problem is, calling it only moves the row, it doesn't update/redraws it. The only way to do so seems to be by finding the cell and calling setNeedsDisplay directly. But this is tricky since you never know whether the cell already got moved or not.
I can't call reloadRowsAtIndexPaths:
either, since I'm not allowed to have 2 animations applied to a single row inside the same beginUpdates-endUpdates
block.
There are may workarounds I can think of, like splitting the whole thing into 2 animation blocks. But I'm wondering whether I'm missing something, as the whole behavior seems odd to me.
回答1:
Finally received an official answer from Apple regarding this issue:
Engineering has determined that this issue behaves as intended based on the following information:
Only insert and update cause the cell to be requested from the data source (the first because it's new and the second because you've explicitly told the table view to update the cell). Delete and move don't update the contents because that's not implicit in the action being performed. If you need to change the contents of the cell during the update, just reach into the cell and change the contents.
回答2:
They like making it difficult for us don't they ;-)
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
// old API
//[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
// new API
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:newIndexPath]; // note use of newIndexPath
[tableView moveRowAtIndexPath:indexPath toIndexPath:newIndexPath]; // NS_AVAILABLE_IOS(5_0);
break;
Note in the ChangeMove, configureCell has different params compared to its use in ChangeUpdate so be careful if copy pasting. Rather tricky to get that right.
来源:https://stackoverflow.com/questions/7861695/moverowatindexpath-doesnt-refresh-the-cells-content