简体   繁体   中英

Add Gesture Recognizer to specify object of cell in table

im trying to add a gesture recognizer to an uiimageview in all cell that will make the uiimageview on that indexpath to change the image, but i cant figure out how to tell the gesture ibaction to change the image in that indexpath.

what i achieve with this code is that it only work fine in the last cell, all other cell not getting the gesture also.

my code:

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

im sorry if the question have been asked before :) but i search for hour but cant find the proper one.

*edit: in this code, every cell got the tap recognizer, but the ibaction is not fired

- (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: i finally found out how to do it, the code from below is correct, but need tell the gesture the action to fire!

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

You should get the image view from the gesture. There is no need to use an instance variable for imgConfirm . You also need to create a separate gesture recognizer for each image view. You can't reuse the same one over and over.

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

One important piece missing here is that if the user scrolls the table, the image will be reset. You need to add more code to keep track of the current state of each image so your cellForRowAtIndexPath: sets the proper image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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