0

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 didSelectRowAtIndexPath 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

Ramprasad A
  • 221
  • 1
  • 13
  • can you give a screenshot of you tableviewcell? – Subhajit Halder Nov 23 '16 at 06:13
  • Is the area with disabled touches separate from the image you reference? So the cell has 3 different areas: 1) triggers didSelectRowAtIndexPath, 2) checks your image, 3) doesn't respond to touches? – dylansturg Nov 23 '16 at 06:23
  • @dylansturg : Yes..! not three, but has two areas, 1. Till the check image , and the 2. The check image area. The problem is if i just tap slightly outside the image didSelectRowAtIndexPath method is calling, this i need to make selectable. – Ramprasad A Nov 23 '16 at 06:48
  • you can add tap gesture on specific area of cell and you don't need to implement didselectrowatindexpath method . – Sumit Jangra Nov 23 '16 at 06:50
  • I am using a custom class for recognition the image tap and getting the proper response too. But as i said above if the user taps slightly outside the image bounds didSelectRowAtIndexPath method is triggered, so that i need to make this area not touchable. – Ramprasad A Nov 23 '16 at 06:52

5 Answers5

2

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
    }
}
1
  1. Set tableview selection style as UITableViewCellSelectionStyleNone

  2. Implement any of the following delegate method, which stops didSelectRowAtIndexPath from getting called.

  3. Set userInteractionEnabled for your check ImageView and implement the method for image tap (Use UIButton or UIImageView with TapGesture)

    - (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
           return nil;    
    }
    

    OR

    - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
         return NO;
    }
    

Hope this helps.

Sailendra
  • 1,318
  • 14
  • 29
0

Please write this code in cellForRowAtIndexPath:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

If you add check box and uncheck box then write this code in cellForRowAtIndexPath

if(indexPath.row == 0){
UIButton *btnForCheckAC = [[UIButton alloc]initWithFrame:CGRectMake(cell.frame.size.width - 50 , 10,15,15)];
btnForCheckAC.tag = BUSTYPEBTN_TAG;
UIImage *image = (isAcButtonClkd)?[UIImage imageNamed:@"check-mark-pink.png"]:[UIImage imageNamed:@"uncheckbox.png"];

[btnForCheckAC addTarget:self action:@selector(busTypebtnAction:event:) forControlEvents:UIControlEventTouchUpInside];
[btnForCheckAC setImage:image forState:UIControlStateNormal];
                cell.accessoryView = btnForCheckAC;
}

Then in didSelectedRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {

       isAcButtonClkd = !isAcButtonClkd;

    }
}

Write the button action:

-(void)busTypebtnAction:(id *)sender event:(id)event{

    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tblBookBus];
    NSIndexPath *indexPath = [self.tblBookBus indexPathForRowAtPoint: currentTouchPosition];
    if (indexPath != nil)
    {
        [self tableView: self.tblBookBus didSelectRowAtIndexPath: indexPath];
    }

}

Change the name according to your requirement.

I hope it will help to you.

Sailendra
  • 1,318
  • 14
  • 29
Sanjukta
  • 1,057
  • 6
  • 16
0

If your check image is UIBUtton then Try this

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.allowsSelection = false;
}

Hope it will help you

jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29
0

First make that imageview as uibutton

and make cellselectionstyle as none.

In cellForRowIndexPath:

{
[cell.CloseButton addTarget:self action:@selector(CrossClicked:) forControlEvents:UIControlEventTouchUpInside];

[cell.CloseButton setTag:indexPath.row];

}

in Method take the tag value for clicked button row number Like this:

-(void)CrossClicked:(UIButton *)sender{

int index = sender.tag;

//on basis of index you can perform all task

}
Sailendra
  • 1,318
  • 14
  • 29
Sagar Snehi
  • 398
  • 3
  • 13