anthoprotic
anthoprotic

Reputation: 827

UITapGestureRecognizer in UITableView - first cell is ignored by tap gesture

I'm trying to make a certain area of my cell respond to a tap gesture. For that, I added a UIView called "touchViewProfile" in my cell. This code has been added as is in 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;
}

It works for every cell except the first one. Did I forget something? I declared "UIGestureRecognizerDelegate" in my .h

EDIT: "touchViewProfile" is a UIView I created with Interface Builder in SimpleTableCell.xib. It's a property, synthesized in .m

Upvotes: 0

Views: 1404

Answers (1)

Prad
Prad

Reputation: 51

You need to have ceell index value first so we need to implement UIGesture 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
   }
}

Upvotes: 1

Related Questions