CGPoint使用touchesBegan点击UITableViewCell

时间:2016-03-31 23:08:31

标签: ios objective-c xcode uitableview

我需要CGPoint点击UITableViewCell。为此,我想在我的子类touchesBegan:withEvent:中使用UITableViewCell方法。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];
    NSLog(@"%@", NSStringFromCGPoint(location));
}

如何将其从我的UITableViewCell子类转发到我当前的viewController,以便我可以根据他们点击的单元格中的位置进行操作?

**我不想在单元格上使用手势识别器,因为它不允许调用didSelectRowAtIndexPath:方法。

1 个答案:

答案 0 :(得分:1)

委托或阻止回拨都会这样做。以下是使用块的示例

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
  CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

  __weak __typeof(self) weakSelf = self;
  cell.touchedAtPoint = ^(UITableViewCell *cell, CGPoint point) {
    [weakSelf doSomethingWithPoint:point);
  };

  return cell;
}

在单元格中

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
{
  [super touchesBegan:touches withEvent:event];

  UITouch *touch = [[event allTouches] anyObject];
  CGPoint location = [touch locationInView:touch.view];

  void (^touchedAtPoint)(UITableViewCell *, CGPoint) = self.touchedAtPoint;
  if (touchedAtPoint) {
    touchedAtPoint(self, location);
  }
}

- (void)prepareForReuse;
{
  [super prepareForReuse];

  self.touchedAtPoint = nil;
}

伊娃宣言

@property (nonatomic, copy) void (^touchedAtPoint)(UITableViewCell *cell, CGPoint point);