添加手势识别器以指定 table 中的单元格对象

Add Gesture Recognizer to specify object of cell in table

我正在尝试向所有单元格中的 uiimageview 添加手势识别器,这将使该索引路径上的 uiimageview 更改图像,但我无法弄清楚如何告诉手势 ibaction 更改该索引路径中的图像。

我用这段代码实现的是它只在最后一个单元格中工作正常,所有其他单元格也没有得到手势。

我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    // Configure the cell...
    imgConfirm = (UIImageView *)[cell viewWithTag:107];
    [imgConfirm setImage:[UIImage imageNamed:@"icon2"]];
    [imgConfirm addGestureRecognizer:self.tapGestureM2];
    return cell;
}

- (IBAction)tapGestureTap:(UITapGestureRecognizer *)sender {
    NSData* imgConfirmData1 = UIImagePNGRepresentation(imgConfirm.image);
    NSData* imgConfirmData2 = UIImagePNGRepresentation([UIImage imageNamed:@"icon2"]);

    if ([imgConfirmData1 isEqualToData:imgConfirmData2]) {
        [imgConfirm setImage:[UIImage imageNamed:@"icon"]];
    }
    else{
        [imgConfirm setImage:[UIImage imageNamed:@"icon2"]];
    }
}

如果之前有人问过这个问题,我很抱歉 :) 但我搜索了几个小时,但找不到合适的。

*编辑:在这段代码中,每个单元格都有点击识别器,但没有触发 ibaction

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
indexpath=[self.tableViewM2 indexPathForCell:cell];
// Configure the cell...
UIImageView *imgConfirm = (UIImageView *)[cell viewWithTag:107];
[imgConfirm setImage:[UIImage imageNamed:@"icon2"]];

UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] init];
// setup gesture as needed
[imgConfirm addGestureRecognizer:gesture];
return cell;
}

- (IBAction)tapGestureTap:(UITapGestureRecognizer *)sender {
NSLog(@"%d,%d",indexpath.row,indexpath.section);
UIImageView *imgConfirm = (UIImageView *)sender.view;
NSData* imgConfirmData1 = UIImagePNGRepresentation(imgConfirm.image);
NSData* imgConfirmData2 = UIImagePNGRepresentation([UIImage imageNamed:@"icon2"]);

if ([imgConfirmData1 isEqualToData:imgConfirmData2]) {
    [imgConfirm setImage:[UIImage imageNamed:@"icon"]];
}
else{
    [imgConfirm setImage:[UIImage imageNamed:@"icon2"]];
}
}

*Edit2:我终于知道怎么做了,下面的代码是正确的,但需要告诉手势触发动作!

UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureTap:)];

您应该从手势中获取图像视图。 imgConfirm 不需要使用实例变量。您还需要为每个图像视图创建一个单独的手势识别器。您不能一遍又一遍地重复使用同一个。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    // Configure the cell...
    UIImageView *imgConfirm = (UIImageView *)[cell viewWithTag:107];
    [imgConfirm setImage:[UIImage imageNamed:@"icon2"]];
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] inittWithTarget:self action:@selector(tapGestureTap:)];
    // setup gesture as needed
    [imgConfirm addGestureRecognizer:gesture];
    return cell;
}

- (IBAction)tapGestureTap:(UITapGestureRecognizer *)sender {
    UIImageView *imgConfirm = (UIImageView *)sender.view;
    NSData* imgConfirmData1 = UIImagePNGRepresentation(imgConfirm.image);
    NSData* imgConfirmData2 = UIImagePNGRepresentation([UIImage imageNamed:@"icon2"]);

    if ([imgConfirmData1 isEqualToData:imgConfirmData2]) {
        [imgConfirm setImage:[UIImage imageNamed:@"icon"]];
    }
    else{
        [imgConfirm setImage:[UIImage imageNamed:@"icon2"]];
    }
}

这里缺少的一个重要部分是,如果用户滚动 table,图像将被重置。您需要添加更多代码来跟踪每个图像的当前状态,以便您的 cellForRowAtIndexPath: 设置正确的图像。