繁体   English   中英

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

[英]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"]];
    }
}

我很抱歉,如果以前已经问过这个问题:)但我搜索了一个小时,但找不到合适的一个。

*编辑:在此代码中,每个单元格都获得了敲击识别器,但未触发

- (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"]];
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM