Reputation: 31975
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?
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
Reputation: 3484
You have three approaches to accomplish it:
Add UITapGestureRecognizer
to your UIImageView
. It will forces the UITableView
dont call didSelectRowAtIndexPath
if you tap over UIImageView
.
If you are using storyboard, you can override override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject!) -> Bool
and avoid it.
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
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