IOS 7 - How to get the indexPath from button placed in UITableViewCell

26,737

Solution 1

NSLog(@"%ld",(long)indexPath); is wrong this will print the pointer address of indexPath

try using following codes

CGPoint center= sender.center; 
  CGPoint rootViewPoint = [sender.superview convertPoint:center toView:self.filterTableView];
  NSIndexPath *indexPath = [self.filterTableView indexPathForRowAtPoint:rootViewPoint];
  NSLog(@"%@",indexPath);

Solution 2

Swift solution: A UITableView extension like this one can be useful for this.

extension UITableView {
    func indexPathForView(view: AnyObject) -> NSIndexPath? {
        let originInTableView = self.convertPoint(CGPointZero, fromView: (view as! UIView))
        return self.indexPathForRowAtPoint(originInTableView)
    }
}

Usage becomes easy everywhere.

let indexPath = tableView.indexPathForView(button)

Solution 3

If you have button in cell. You can get cell by calling superview. And then can get Indexpath by this way.

 (void)obButtonTap:(UIButton *)sender {

    UITableViewCell *cell = (UITableViewCell *)sender.superview;
    NSIndexPath *indexPath = [tableView indexPathForCell:cell];
}

Solution 4

You can assign tags to each switch in cellForRowAtIndexPath method like

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    
   UISwitch *accessorySwitch = [[UISwitch alloc]initWithFrame:CGRectZero];
   [accessorySwitch setOn:NO animated:YES];
   [accessorySwitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];
   cell.accessoryView = accessorySwitch; 
   accessorySwitch.tag = indexPath.row;    
}

- (void)changeSwitch:(UISwitch *)sender{
    NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:[sender tag]];
    NSLog(@"%ld",(long)indexPath);
}

Solution 5

SWIFT 4 :

extension UITableView {
    func indexPathForView(view: AnyObject) -> NSIndexPath? {
        let originInTableView = self.convert(CGPoint.zero, from: (view as! UIView))
        return self.indexPathForRow(at: originInTableView)! as NSIndexPath
    }
}
Share:
26,737
Arun
Author by

Arun

iOS Developer

Updated on June 11, 2021

Comments

  • Arun
    Arun almost 3 years

    I've programmatically created a UITableView and added UISwitch to it's cell accessory view.

    This is my code for UISwitch in cell accessory view in cellForRowAtIndexPath method.

    UISwitch *accessorySwitch = [[UISwitch alloc]initWithFrame:CGRectZero];
    [accessorySwitch setOn:NO animated:YES];
    [accessorySwitch addTarget:self action:@selector(changeSwitch:) forControlEvents:UIControlEventValueChanged];
    cell.accessoryView = accessorySwitch;
    

    This is the method which is called after the button is clicked.

    - (void)changeSwitch:(UISwitch *)sender{
    
        UITableViewCell *cell = (UITableViewCell *)[sender superview];
    
        NSIndexPath *indexPath = [self.filterTableView indexPathForCell:cell];
        NSLog(@"%ld",(long)indexPath);
    
     ……………. My other code…….
    }
    

    I am able to print the index path value in iOS 6 But in iOS 7 it prints nil,

    Am i missing anything in iOS 7 or there is some other approach to get the indexPath in iOS 7

    Thanks, Arun.