Я пытаюсь выполнить резервное копирование своих основных данных. UITableView имеет возможность переупорядочения. После реализации всех этих делегатов и некоторых методов для основных данных, упомянутых здесь, я обнаружил странное поведение. После нажатия кнопки редактирования значок переупорядочения отображается просто отлично, и я могу нажать на него, тень появляется, но когда я пытаюсь переместить его в другую строку, я внезапно теряю фокус, и ячейка возвращается на свое место. Кто-нибудь знает причину этой проблемы?
Вот видео, показывающее проблему
http://www.youtube.com/watch?v=ugxuLNL7BnU&feature=youtu.be
Вот мой код
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
in -tableView:moveRowAtIndexPath:toIndexPath
Я пробовал код ниже и только пустую реализацию, но проблема все еще существует, поэтому я не думаю, что это не проблема.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
_changeIsUserDriven = YES;
NSUInteger fromIndex = sourceIndexPath.row;
NSUInteger toIndex = destinationIndexPath.row;
NSMutableArray *newsArray = [[self.fetchedResultsController fetchedObjects] mutableCopy];
News *news = [self.fetchedResultsController.fetchedObjects objectAtIndex:fromIndex];
[newsArray removeObject:news];
[newsArray insertObject:news atIndex:toIndex];
int i = 1;
for (News *n in newsArray) {
n.displayOrder = [NSNumber numberWithInt:i++];
}
[self saveContext];
_changeIsUserDriven = NO;
}
делегат FetchedResultController
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
if (_changeIsUserDriven) {
return;
}
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
if (_changeIsUserDriven) {
return;
}
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
if (_changeIsUserDriven) {
return;
}
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
if (_changeIsUserDriven) {
return;
}
[self.tableView endUpdates];
}