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

时间:2015-04-24 03:50:20

标签: ios objective-c uitableview uitapgesturerecognizer

我试图将手势识别器添加到所有单元格中的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:)];

1 个答案:

答案 0 :(得分:1)

您应该从手势中获取图像视图。无需为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"]];
    }
}

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