简体   繁体   中英

IOS - UITableView Cell, click on UIImageView inside cell but stop didSelectRowAtIndexPath being called

I have a Custom Cell and when you tap on the Custom Cell another View appears, but in this Custom Cell i have a Flag ImageView. What i need to do is if the Flag ImageView is tapped don't goto didSelectRowAtIndexPath as i need to change the Flag ImageView to a red flag and stop the View from appearing when the Flag ImageView is clicked. So almost like Javascript you use stopPropagation(), is there a way to do this in IOS using Custom Cells?

You need to set the userinteraction enable yes. set

cell.imageView.userInteractionEnabled = YES;

then it will not call the didselect method of tableview

Here you can't stop it directly if you have the UIImageView inside the cell. So, instead of UIImageView you can go with custom cell and take UIButton. That will allow you to solve the issue in didSelectRowAtIndexPath.

So, when we click on UIButton it will give higher priority to Button first than the UITableViewCell Row. If that won't work for you then please provide your feedback and I will solve your issue as soon as possible.

Use a UIButton for the Flag. Creating a IBOutlet for this button and setting the background to be what ever you want.

In interface builder you will need to assign your button a tag number, then inside your Tableview cellForRowAtIndexPath instantiate it like so.

    UIButton *favs = (UIButton *)[cell viewWithTag:105];

And then implement a target action inside the cellForRowAtIndexPath method as well for the button like:

[favs addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];

Then implement a method for you checkButtonTapped passing in the touch event like:

    - (void)checkButtonTapped:(id)sender event:(id)event{

    //Create touch set
    UITouch *touch = [[event allTouches] anyObject];

    //Get Current touch position
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    //Get IndexPath
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
}

Then do whatever you need to do to change the background of the Unbutton to a red flag using the indexPath from the touch point.

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