如何从UITapGestureRecognizer的句柄功能获取UITableViewCell?

时间:2014-05-19 04:22:25

标签: uitableview

我在UITavleViewCell中添加了UIImageView,并为UIImageView添加了UITapGestureRecognizer,如何在UITap事件处理函数(void)likeImageTap:(UITapGestureRecognizer*)tap中获取UITableViewCell?

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* CellIdentifier = @"TextCellIdentifier";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    };

    UIImageView* likeImage = (UIImageView*)[cell.contentView viewWithTag:kLikeImage];
    [likeImage setUserInteractionEnabled:YES];
    UITapGestureRecognizer *likeImageTap =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(likeImageTap:)];
    [likeImageTap setNumberOfTapsRequired:1];
    [likeImage addGestureRecognizer:likeImageTap];

    return cell;
}


- (void)likeImageTap:(UITapGestureRecognizer*)tap {
    // How cam i get UITableViewCell?
    UIImageView* likeImageView = (UIImageView*)[cell viewWithTag:kLikeImage];
    [likeImageView setImage:[UIImage imageNamed:@"ic_liked"]];
}

1 个答案:

答案 0 :(得分:0)

CGPoint loc = [tap locationInView:self.tableView];
for (UITableViewCell* cell in self.tableView.visibleCells){
    if (CGRectContainsPoint(cell.frame, loc)){
        // cell is the cell you're after
    }
}