Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

indexpath row not getting in prepare for segue for detail disclosure

 if([segue.identifier isEqualToString:@"Edit"]){

    AddCoffeeViewController *avc = [segue destinationViewController];

    Coffee *coffeeObj = [appDelegate.coffeeArray objectAtIndex:arow];
    NSLog(@"ViewController.m prepareForSegue: arow: %d coffeeName:%@ price:%@      coffeeID:%d",arow,coffeeObj.coffeeName,coffeeObj.price,coffeeObj.coffeeID);
    [coffeeObj setIsInEditMode:YES];
    avc.EditCoffeeObj = coffeeObj;


}

I am not getting the correct row when i am pressing accessory button.

Although in below method i am getting the correct row. Is there any way that i can get correct row prepare for segue if i do not want to use any ivar.

-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{

arow = indexPath.row;

}

enter image description here


tableViewCell accessory button: Performing modal segue(Edit)
+ Button (Navigation bar button Item): Performing modal segue(Add)

like image 481
bhavya kothari Avatar asked Feb 11 '13 14:02

bhavya kothari


People also ask

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

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.

What does IndexPath row return?

indexPath(for:) Returns an index path that represents the row and section of a specified table-view cell.

What is IndexPath row?

Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section. For example, the first row in a table would have section 0, row 0, whereas the eighth row in the fourth section would have section 3, row 7.

How do I create an IndexPath in Objective C?

To create an IndexPath in objective C we can use. NSIndexPath *myIP = [NSIndexPath indexPathForRow: 5 inSection: 2] ; To create an IndexPath in Swift we can use.


2 Answers

If you have hooked up accessory action in your storyboard then the sender in prepareForSegue:sender: will be the cell that that was tapped. This means you can just do something like

NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
like image 109
Paul.s Avatar answered Sep 17 '22 12:09

Paul.s


Accessory button tapped and selected row are two different things. In the accessoryButtonTappedForRowWithIndexPath, just set the indexPath to a global variable and then use that in the segue.

like image 38
Kris Gellci Avatar answered Sep 16 '22 12:09

Kris Gellci