如何在UITableViewCell中只添加一个tapGesture到图像?

时间:2016-08-30 21:53:28

标签: ios objective-c uitableview

我有tableView个自定义单元格。我在一个单元格中有一个imageView。我想添加一个imageView一个tapGesture来加载一个函数,然后点击imageView。所以我将此代码放入cellForRowAtIndexPath:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
singleTap.numberOfTapsRequired = 1;
[cell.image setUserInteractionEnabled:YES];
[cell.image addGestureRecognizer:singleTap];

-(void)tapDetected:(id)sender{
    NSLog(@"single Tap on imageview");
}

我的问题是,当我滚动表格视图时,会多次添加此点按并更改为imageView,然后首先点击更改。

如何更改tapGesture只向所有单元格添加一次?

4 个答案:

答案 0 :(得分:0)

如果您有自定义单元格,则可以使用以下函数:

-(void) addTapGesture:(UITapGestureRecognizer*)tapGesture {
    if (self.tapGesture == nil) {
        [self.image setUserInteractionEnabled:YES];
        [self.image addGestureRecognizer:singleTap];

        self.tapGesture = tapGesture
    }
}

通过这种方式,您确定每个单元格只设置一次tapGesture。

如果这可以解决您的问题,请告诉我

答案 1 :(得分:0)

问题

现在,每次调用cellForRowAtIndexPath时都会添加一个新的TapGestureRecognizer,这是每次一个单元格即将出现在屏幕上时!

解决方案1:保留旧手势识别器

您还可以检查单元格是否已包含手势识别器,如果有,则不添加新手势识别器。这将确保每个单元格只应用一个TapGestureRecognizer:

// Check if the cell already contains a gesture recognizer
//  if not, add one! 
if (cell.gestureRecognizers.count == 0) {
    // Now add the tap gesture recognizer and it will be the only one
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
    singleTap.numberOfTapsRequired = 1;
    [cell.image setUserInteractionEnabled:YES];
    [cell.image addGestureRecognizer:singleTap];
}
  

注意:您应该检查并确保单元格包含   确切地说你想要的手势识别器 - 你可能想要添加   更多的手势识别器,然后你必须做   确定它包含正确的!

解决方案2:删除手势识别器

在添加TapGestureRecognizer之前,从单元格中删除所有手势识别器。这将确保每个单元格只应用一个TapGestureRecognizer:

// Removes all gesture recognizers in the cell
for (UIGestureRecognizer *recognizer in cell.gestureRecognizers) {
    [cell removeGestureRecognizer:recognizer];
}

// Now add the tap gesture recognizer and it will be the only one
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
singleTap.numberOfTapsRequired = 1;
[cell.image setUserInteractionEnabled:YES];
[cell.image addGestureRecognizer:singleTap];

答案 2 :(得分:0)

就在您的自定义单元格中,执行此操作:(我使用swift作为示例)

class YourCell:UITableViewCell{
//add a variable to track has added a tapgesture to image view
var isAddedTapGesture = false
......
}
你的cellForRowAtIndexPath中的

if(cell.isAddedTapGesture == false){
//add the tap gesture to your image
cell.cell.isAddedTapGesture = true
}

答案 3 :(得分:0)

在自定义单元格标题中,创建一个类似

的属性

@property(强大,非原子)UITapGestureRecognizer * tapGesture;

为手势添加Getter方法。

- (UITapGestureRecognizer*)tapGesture{
    if (!_tapGesture) {
        _tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
    }

    return _tapGesture;
}

并在awakeFomNib函数内部,删除并重新添加图像视图的手势,如下所示。

[self.image removeGestureRecognizer:self.tapGesture];
[self.image addGestureRecognizer:self.tapGesture];

希望这会有所帮助。