检测UITableView的自定义单元格上的水龙头

时间:2014-12-12 08:40:17

标签: ios objective-c uitableview

所以我有一个自定义UITableViewCell,其中包含三个UILabelUITableViewCell使UILabel完全覆盖单元格。

现在我想检测用户点击单元格的时间。问题是因为UILabel覆盖了我无法使用UITableView委托方法来检测单元格上的触摸的单元格。

我虽然在UILabel上使用手势识别器但是我没有得到触摸的单元格的索引。我还想过在单元格顶部放置一个透明按钮,但是这里也存在同样的问题,我无法获得触摸单元格的索引。

任何人都可以指导我如何在tableView didSelectRowAtIndexPath覆盖单元格时使用UILabel检测点击次数。

3 个答案:

答案 0 :(得分:0)

尝试在单元格中的所有标签中将userInteractionEnabled设置为false。这会将触摸传递给单元格,然后您可以使用UITableView委托。确保单元格保持userInteractionEnabled = YES

答案 1 :(得分:0)

要获得除didSelect方法之外的触摸事件,请尝试此操作。在您的自定义单元格类中添加一个块,如

typedef void(^TappedCell)(CustomCell *cell);

并为其添加属性

@property (nonatomic, strong) TappedCell tappedCell;

和透明按钮操作

- (IBAction)buttonTapped {

    if (self.tappedCell) {
        self.tappedCell(self);
    }
}

cellForRow方法

__weak type(self)weakSelf = self;
cell.tappedCell = ^ (CustomCell *tappedCell) {

     //Ur actions goes here
     //for getting index
     NSIndexPath *indexPath = [weakSelf.tableView indexPathForCell:tappedCell];
     [weakSelf someActionWithIndexPath:indexPath];
};

答案 2 :(得分:0)

UILabel隐藏您的单元格或UIImageView隐藏它是否无关紧要。它会自动检测tableView didSelectRowAtIndexPath。先检查一下你不会遇到任何问题。您是否首先尝试详细实现tableview