Blaszard
Blaszard

Reputation: 31975

Let "didSelectRowAtIndexPath" NOT called only when an image within cell is tapped (but make it called otherwise)?

I would like to put an image on my UITableViewCell, and make the cell moved to detail view controller when an user taps on the cell. However, I also want to let the cell NOT move to the detail VC when an user taps on the image which is put on only the part of the cell. The cell looks something like the follows:

[cell.textLabel     myImage    accessoryType] <- UITableViewCell

However, when I implement my code like below, an user must be moved to the detail VC when she taps on the cell, even if she taps on the tiny portion covered by the image. Is it possible to disable the didSelectRowAtIndexPath: when she taps on the image?

tableView:didSelectRowAtIndexPath:

var cell = self.tableView.dequeueReusableCellWithIdentifier(CellReuseIdentifier, forIndexPath: indexPath) as! UITableViewCell

var list = listArray[indexPath.row]
cell.textLabel?.text =  list.name

cell.accessoryType = UITableViewCellAccessoryType.DetailButton
let image = UIImage(named: "myImage.png")
var imageView = UIImageView(image: image)
imageView.frame = CGRectMake(280, 4, 36, 36)
cell.addSubview(imageView)

return cell

I tried imageView.userInteractionEnabled = false but the result didn't change.

Also note that the image is not accessory view since I also want to use accessory type (both can't be used) so you can't use tableView:accessoryButtonTappedForRowWithIndexPath: delegate method.

Upvotes: 0

Views: 557

Answers (2)

Klevison
Klevison

Reputation: 3484

You have three approaches to accomplish it:

  1. Add UITapGestureRecognizer to your UIImageView. It will forces the UITableView dont call didSelectRowAtIndexPath if you tap over UIImageView.

  2. If you are using storyboard, you can override override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject!) -> Bool and avoid it.

  3. Or as @DuncanLowrie said:

    use a UIButton instead of an image view, or overlay the the image with a button. The button doesn't have to do anything, but will intercept the touch.

Upvotes: 5

Duncan Lowrie
Duncan Lowrie

Reputation: 116

One way to achieve this would be to use a UIButton instead of an image view, or overlay the the image with a button. The button doesn't have to do anything, but will intercept the touch.

Upvotes: 2

Related Questions