我在 UITableViewCell 上添加了一个手势识别器,以将单元格拖动到同一视图控制器中的其他视图。为此,我在单元格中添加了一个长按手势识别器。
但是当我们不拖动单元格时,我还需要滑动来删除功能。但是,当我开始拖动并沿一个方向(向右或向左)移动时,单元格也会触发滑动删除(这是默认设置)的调用。
我现在需要停止这个调用。重新加载 tableView 不是一个选项,因为重新加载后我会松开选定的单元格进行拖动。
试试下面的代码,它是 ios 中可用的 UITableViewDelegate 方法之一
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Detemine if it's in editing mode
if (self.editing)
{
return UITableViewCellEditingStyleDelete; //enable when editing mode is on
}
return UITableViewCellEditingStyleNone;
}
您还可以使用此方法中可用的indexPath关闭滑动以删除特定部分中的特定行
斯威夫特 3 版本:
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
if tableView.isEditing {
return .delete
}
return .none
}
一些猜测,如果它们中的任何一个对你有用 -
tableView:shouldIndentWhileEditingRowAtIndexPath:
委托方法。现在,您可以在这里做的是增加长按时间或只是添加检测多个手势的功能。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}