ios 7 在表格视图中删除行时出现不稳定的动画问题
Posted
技术标签:
【中文标题】ios 7 在表格视图中删除行时出现不稳定的动画问题【英文标题】:ios 7 unstable animation issue when deleting a row in a table view 【发布时间】:2014-01-07 19:53:35 【问题描述】:我有一个带有搜索栏/显示控制器的表格视图(全部内置故事板)。 我的问题是删除行。删除行的功能有效,但行删除的动画似乎不稳定。有时删除是动画的,大多数情况下该行会立即被删除,没有动画。
这是我的删除方法:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
if (editingStyle == UITableViewCellEditingStyleDelete)
if (tableView == self.searchDisplayController.searchResultsTableView)
[_managedObjectContext deleteObject:[_notesFilteredFetcher objectAtIndexPath:indexPath]];
else
[_managedObjectContext deleteObject:[_notesFetcher objectAtIndexPath:indexPath]];
NSError *error;
if (![_managedObjectContext save:&error])
DLog(@"Failed deleting note : %@", [error localizedDescription]);
表视图正在使用核心数据,因此行的删除发生在适当的控制器方法中,如下所示:
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
UITableView *tableView = (controller == _notesFilteredFetcher) ? self.searchDisplayController.searchResultsTableView : 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 forTable:tableView];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray
arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray
arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
我还注意到,当删除动画确实发生时,已删除行上方的所有行都会向下移动一点,而已删除行下方的行会向上移动,直到空白空间被覆盖。
如果我查看内置 mail 应用程序并删除一些行(即邮件),它似乎每次都按预期进行动画处理,并且只有已删除行下方的行被向上移动以覆盖空白空间(已删除行上方的行不移动)。
有什么想法吗?
更新:
我注意到动画只发生在最后一行。如果我删除任何其他行,则不会出现动画,但如果我删除最后一行动画确实有效。
【问题讨论】:
【参考方案1】:如果有人遇到同样的问题:
我注意到我在调用同一表视图的endUpdates
之后立即调用了表视图的reloadData
方法,因为我需要更新单元格的内部索引。
这将导致动画立即停止。删除 reloadData
方法解决了动画问题。
【讨论】:
以上是关于ios 7 在表格视图中删除行时出现不稳定的动画问题的主要内容,如果未能解决你的问题,请参考以下文章
删除按钮出现时自定义表格视图单元格中缺少 UISlider 宽度的动画
删除行时的 EXC_BAD_ACCESS(代码=EXC_I386_GPFLT)(在:[IndexPath])