我正试图让我的核心数据支持的UITableView具有重新排序能力,在实现了这里提到的所有这些委托和一些核心数据技术后,我发现了奇怪的行为。点击编辑按钮后,重新排序图标显示得很好,我可以点击它,阴影就会出现,但当我试图将它移动到另一行时,我突然失去了焦点,单元格移回了原来的位置。有人知道这个问题的原因吗?
这是显示问题的视频
http://www.youtube.com/watch?v=ugxuLNL7BnU&feature=youtu.be
这是我的代码
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
在-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];
}
最后我发现问题是因为我使用的外部库IIViewDeckController,在删除后问题就消失了。
这确实是由于ViewDeckController。我正在MonoTouch中使用一个端口,但仍然遇到这个问题。这要归功于(更具体地说)UIPanGestureRecognizer。手势识别器正在接收平移/拖动移动,并认为这是为自己准备的,因此它取消了发送到UITableView的触摸。
不幸的是,没有UIPanGestureRecognizer.direction(horizontaly/vertical)属性,但这里有一篇帖子:UIPanGe斯图reRecognizer-关于如何将平移手势限制在某个方向,只有垂直或水平。您需要修改ViewDeckController代码,其中添加了UIPanGestureRecognizer以使用上面问题中的子类版本。
我还没有对此进行测试(就像今天凌晨2点我所做的那样!),但一旦我有时间实现它,我会更新我的答案。
作为一个快速而肮脏的修复,你可以设置
panner.cancelsTouchesInView = NO;
这将允许你移动你的项目,但它也会有其预期的结果,即不取消视图中的触摸,因此,根据你的中心视图的内容,它可能会导致你的用户在只想将视图组滑动到一边时按下按钮。
另一种可能性是,当您将表设置为编辑模式时,将此属性设置为NO,并在完成后将其更改回YES。