Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Indexpath of cell in UITableView while clicking on Custom accessory Button?

I have UITableView.In that i have created custom cell with accessory button.Now by clicking on accessory button i want to create another view with edit cell functionality.For that how do i find the index path of that cell?and how do i pass the vales of that cell?

How do i call following method:

 - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
like image 704
Developer Avatar asked Oct 17 '11 05:10

Developer


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.


2 Answers

First thing to note here is that when you use custom accessoryView in a cell then the tableView:accessoryButtonTappedForRowWithIndexPath: delegate method would not be called. You have to add some target/action to the button you are adding as the custom accessory and handle the tap action yourself. You should add the custom accessory something like this,

UIButton *accessory = ...; [accessory addTarget:self action:@selector(onCustomAccessoryTapped:) forControlEvents:UIControlEventTouchUpInside]; ... cell.accessoryView = accessory; 

And in the onCustomAccessoryTapped: method you have to get the index path like this,

- (void)onCustomAccessoryTapped:(UIButton *)sender {      UITableViewCell *cell = (UITableViewCell *)sender.superview;     NSIndexPath *indexPath = [tableView indexPathForCell:cell];      // Now you can do the following     [self tableView:tableView accessoryButtonTappedForRowWithIndexPath:indexPath];      // Or you can do something else here to handle the action } 
like image 114
EmptyStack Avatar answered Oct 12 '22 00:10

EmptyStack


The indexPath is passed as the second parameter to the method.

like image 45
Dave DeLong Avatar answered Oct 12 '22 00:10

Dave DeLong