Ramprasad A Ramprasad A - 7 months ago 76
Swift Question

How to disable tableViewCell selection for a particular part in the UITableView cell iOS

I am new to iOS, now I have a requirement to perform two actions associated with a UITableView cell, I have a check image in my cell, once i click on it i am getting a touch response, but if i tap anywhere other than the image area didSelectRowAtIndexPath method is getting called.

What should i do to achieve calling

for only a selected area inside the cell and ignore the remaining area, or disable touches to in few area of cell.

My layout looks like this


I understand your problem. you have to override the touchesBegan(touches: Set, withEvent event: UIEvent?) method in your custom UITableViewCell.

 class CustomTableViewCell: UITableViewCell {
   var isValidTouch: Bool = true

   override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    if let touch = touches.first {
       let point = touch.locationInView(self.contentView)
       let validTouchPoint = self.contentView.bounds.width - (checkMarkView.bounds.width + 16.0) //Trailing + Leading space
        if point.x < validTouchPoint {
            isValidTouch = true
        } else {
            isValidTouch = false
            //Send tap gesture callback to your viewcontroller
    super.touchesBegan(touches, withEvent: event)


In your viewcontroller UITableViewDelegate method just check your touch is valid.

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let cell = tableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell
    if cell.isValidTouch {
      //do whatever