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;
}
tableViewCell accessory button: Performing modal segue(Edit)
+ Button (Navigation bar button Item): Performing modal segue(Add)
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.
indexPath(for:) Returns an index path that represents the row and section of a specified table-view cell.
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.
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.
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];
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With