【问题标题】:Prevent UITableViewCell selection when dragging a subview拖动子视图时防止 UITableViewCell 选择
【发布时间】:2013-05-02 12:46:59
【问题描述】:

我有一个UITableViewCell 子类,并且我在其contentView 属性中添加了一个UIView。为了能够拖动该子视图,我实现了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];
}

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

如何防止在拖动子视图时单元格被选中?

【问题讨论】:

    标签: ios uiview uitableview touches


    【解决方案1】:
    -[UITableViewCell setSelectionStyle: UITableViewCellSelectionStyleNone]
    

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

    【讨论】:

      【解决方案2】:

      在不了解更多信息的情况下无法准确判断g,但有几种方法可以做到这一点:

      1. 您可以通过tableView:willSelectRowAtIndexPath:阻止UITableViewDelegate协议中的选择

      2. 您可以将表格置于编辑模式并使用allowsSelectionDuringEditing

      3. 您可以通过覆盖 [UITableViewCell setSelected:animated:][UITableViewCell setHighligted:animated:] 来阻止选择/突出显示 UI。但仍会选择该单元格。

      4. 您可以禁用默认表选择并使用您自己的UITapGestureRecognizer(使用[UITableView indexPathForRowAtPoint:] 非常简单)。使用自定义识别器,您可以使用它的委托来决定您的表格何时收到触摸。

      【讨论】:

        【解决方案3】:

        为了解决这种情况,我为子视图编写了一个协议:

        @protocol MyViewDelegate <NSObject>
        
        ///
        /// Tells the delegate that touches has begun in view
        ///
        - (void)view:(UIView *)view didBeginDragging:(UIEvent *)event;
        
        ///
        /// Tells the delegate that touches has finished in view
        ///
        - (void)view:(UIView *)view didFinishDragging:(UIEvent *)event;
        
        @end
        

        然后我像这样在子视图中完成了UIResponder 方法:

        - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
          [self.delegate view:self didBeginDragging:event];
        }
        
        - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
          self.frame = self.rootFrame;
          [self.delegate view:self didFinishDragging:event];
        }
        

        最后,我将单元格设置为视图的委托,并在拖动手势进行时临时取消单元格选择:

        - (void)view:(UIView *)view didBeginDragging:(UIEvent *)event {
          [self setHighlighted:NO animated:NO];
          [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        }
        
        - (void)vView:(UIView *)view didFinishDragging:(UIEvent *)event {
          [self setSelectionStyle:UITableViewCellSelectionStyleGray];
        }
        

        就是这样

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-01-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-08
          • 1970-01-01
          • 2011-10-26
          相关资源
          最近更新 更多