Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 931
Arun Avatar asked Sep 25 '13 08:09

Arun


People also ask

How do you get the IndexPath row when a button in a cell is tapped?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

How do I get last IndexPath in Swift?

You can get the indexPath of the last row in last section like this. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(numberOfRowsInLastSection - 1) inSection:(numberOfSections - 1)]; Here, numberOfSections is the value you return from numberOfSectionsInTableView: method.

What is IndexPath in Tableview Swift?

Swift version: 5.6. Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section.


1 Answers

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); 
like image 153
lanbo Avatar answered Sep 28 '22 16:09

lanbo