ios - UITableView 中的 UITapGestureRecognizer - 第一个单元格被点击手势忽略

标签 ios objective-c uitableview uigesturerecognizer

我正在尝试让我的单元格的某个区域响应点击手势。为此,我在单元格中添加了一个名为“touchViewProfile”的 UIView。此代码已按原样添加到 cellForRowAtIndexPath 中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:1];
    }

     // Tag Gesture Recognizer for Profile
        UITapGestureRecognizer *tapProfile = [[UITapGestureRecognizer alloc]
                                              initWithTarget:self
                                              action:@selector(showProfile)];
        tapProfile.numberOfTapsRequired = 1;
        tapProfile.numberOfTouchesRequired = 1;
        cell.touchViewProfile.userInteractionEnabled = YES;
        [cell.touchViewProfile addGestureRecognizer:tapProfile];

    return cell;
}

它适用于除第一个单元格之外的每个单元格。我忘了什么吗?我在 .h 中声明了“UIGestureRecognizerDelegate”

编辑:“touchViewProfile”是我在 SimpleTableCell.xib 中使用 Interface Builder 创建的 UIView。这是一个属性,在 .m 中合成

最佳答案

您首先需要有单元格索引值,因此我们需要实现 UI Gesture Delegate

-(void)showProfile:(UIGestureRecognization *)recognize{
  if(recognize.state==UIGestureRecognizeStateEnded){
     CGPoint touched=[recognize locationInVie:self.tableView];
     NSIndexPath *touchIndex=[self.tableView indexPathForRowAtPoin:touched];
     SimpleTableViewCell *touchedCell=[self.tableView cellForRowAtIndexPath:touchedIndex];
     //your code
   }
}

关于ios - UITableView 中的 UITapGestureRecognizer - 第一个单元格被点击手势忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21356138/

相关文章:

ios - 等边距布局 UICollectionViewCells

iphone - 如何使用 UIScrollView 创建顶部淡入淡出效果?

objective-c - 基本内存管理 : Calling [super dealloc] in dealloc method

ios - UIScrollview 中的 ContentOffset 问题

objective-c - CGPathRef 和 NSBezierPath 有什么区别?

ios - 两个 UIViewController 的不同 UITableView 中的一个自定义 UITableViewCell

ios - 在 iOS 中显示多列表格的最有效方法是什么?

ios - 方向 : portrait & landscape in Xcode 4. 2

ios - 从另一个 View 调用 UITableView 函数

iphone - UITableviewcell 内容更新反射(reflect)到其他单元格