我想实现一个方法,以便能够重新排序tableview的行。 在每个部分我只有一行。启用tableview的编辑状态后,移动指示符将显示为已过期。但当我将其中一个细胞拖到另一个位置时,细胞立即(大约1秒后)弹回到原始位置。这对我来说很合适,因为我已经实现了这样的重新排序功能。这两个项目之间的区别在于新项目实现了一堆手势识别器(例如 UILongPressGestureRecognizer , UIPanGestureRecognizer ,UIPinchGestureRecognizer,...)。我已经考虑过其中一个手势识别器阻止了tableviewcell的拖动动作但是没有。
您可以看到以下代码:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
//return [NSIndexPath indexPathForRow:0 inSection:3];
return nil;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSLog(@"%@", destinationIndexPath);
}
任何人都可以帮我吗?
更新:
我发现移动的tableviewcell正在移动到原始目标索引路径。这是否表明表视图上的reloadData已经发生?
第二次更新:
因为Andrei Shender为什么我会在 tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:方法中返回nil,你可以在下面找到我更新的代码。
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
return proposedDestinationIndexPath;
//for testing purpose only
//return nil;
}
当我改变我的代码时,我发现,这个方法永远不会被调用。关于Apples documents about reordering tableviewcells它应该被调用。
答案 0 :(得分:3)
感谢我posting我想通了,问题是什么。将UIPanGestureRecognizer和UITableView与重新排序/移动功能相结合是一件棘手的事情。
当我编辑tableview时,我删除了手势识别器(viewcontroller的视图),当再次禁用编辑时,我再次添加手势识别器。
答案 1 :(得分:0)
请务必使用以下方法更新数据源:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath