ios - 拖动 subview 时防止 UITableViewCell 选择

标签 ios uiview uitableview touches

我有一个 UITableViewCell子类,我添加了 UIView到它的contentView属性(property)。为了启用拖动该 subview ,我实现了 UIResponder适当的方法:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  UITouch *touch = [touches anyObject];
  CGPoint currentLocation = [touch locationInView:self.superview];
  if (currentLocation.x >= self.rootFrame.origin.x) {
    return;
  }

  CGRect frame = self.frame;
  frame.origin.x = currentLocation.x;
  self.frame = frame;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  self.frame = self.rootFrame; // rootFrame is a copy of the initial frame
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
 [self touchesEnded:touches withEvent:event];
}

subview 可以毫无问题地拖动,但单元格也被选中,因此 -tableView:didSelectRowAtIndexPath:正在被调用。

如何防止在拖动 subview 时单元格被选中?

最佳答案

-[UITableViewCell setSelectionStyle: UITableViewCellSelectionStyleNone]

当然,tableView:didSelectRowAtIndexPath:仍将被调用 - 因此您可能希望有选择地忽略回调。

关于ios - 拖动 subview 时防止 UITableViewCell 选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16338457/

相关文章:

ios - 关闭 subview 时如何设置计时器

ios - UITableView 背景上的 UIGestureRecognizer

ios - 从程序执行 .addSubview 到 View 实际出现的意外延迟 - Swift

ios - 为每个 TableView 单元执行不同的 segue

ios - 如何在 Swift iOS 中通过 Pincode 获取城市、州和国家名称?

ios - MFMessageComposeViewController 打开彩信编辑而不是短信和 'buddy name' 文本而不是电话号码

ios - UIScrollView contentOffset 在推送另一个 View 后发生变化

ios - 单个 Storyboard swift 中的多个 TableView

ios - 使一个单元格(但不是第一个单元格)在 UITableView 上始终可见

ios - 调用 FBSDKGameRequestContent : receiving the invite but not the notification in Facebook