UITableView中的UITapGestureRecognizer - 点按手势会忽略第一个单元格

时间:2014-01-25 20:55:21

标签: 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

合成的属性

1 个答案:

答案 0 :(得分:1)

您需要先获得单元格索引值,因此我们需要实现UI手势代理

-(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
   }
}