2

我有一个UITableView具有不同高度的某些单元格。如果单元格中的触摸低于某个点,我希望有一种方法,无需向每个单元格添加按钮,就不会收到对 didSelectedRowAtIndexPath 的响应。

例如,假设我有两个单元格,一个高度为 100,另一个高度为 150。有没有办法不接收对didSelectRowAtIndexPath:低于 100 的触摸的响应,但仍接收单元格上的触摸输入?

我想覆盖单元格中的触摸方法并将委托中的接触点返回到控制器/表并使用该输入来确定我是否会忽略didSelectRowAtIndexPath:响应,但我担心突出显示和其他调用以及它是否可能不够快以阻止这些。这对我来说似乎真的很可疑。

4

3 回答 3

3

我认为如果不对单元进行子类化,这是不可能的。

我的建议是这样做:

  1. 创建细胞的子类
  2. 向单元格添加额外的布尔 iVar,例如 didTapOutside
  3. touchesBegan根据方法中的 Y 位置抽头,将布尔值设置为 True 或 False
  4. didSelectRowAtIndexPath读取 didTapOutside 布尔变量并根据它的设置执行您的操作

示例如下:

@interface MyCustomCell : UITableViewCell {
    BOOL didTapOutside;
}
@property (atomic, readwrite) BOOL didTapOutside;

和实施:

@implementation MyCustomCell

@synthesize didTapOutside;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:touch.view];

    didTapOutside =  (location.y>100);

    [super touchesBegan:touches withEvent:event];
}
于 2013-01-01T20:51:34.863 回答
2

您可以创建自定义UITableViewCell,例如:

CustomCell *cell = [[CustomCell alloc] initWithFrame:rect reuseIdentifier:identifier];

您可以覆盖 CustomCell 中的 touchesBegan 方法并执行以下操作来获取触摸的位置:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *aTouch = [touches anyObject];
    CGPoint point = [aTouch locationInView:self];
    // point.x and point.y have the coordinates of the touch

    // based on where the touch is... your custom code

    [super touchesBegan:touches withEvent:event];
}
于 2013-01-01T20:45:02.213 回答
0

对不起,我一开始读错了这个问题。对此没有简单的解决方案。

如果您不想使用子类,您唯一可以做的就是在要检测触摸的区域上添加一个透明视图或按钮,并在其上创建一个事件,而不是使用表格视图单元格方法。

于 2013-01-01T20:58:39.807 回答