I have a problem with the UItableViewCell accessory button. My accessory button is "connected" with a segue to another UIViewController. When I tap on an accessory button, I get the object from the model and I "pass" this object to the segue destinationViewController. This is my prepare for segue method:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"IdentityCard"])
{
NSLog(@"%i" ,self.tableView.indexPathForSelectedRow.row);
[segue.destinationViewController showIdentityCardForTeacher:[self.teachers getTeacherInPosition:self.tableView.indexPathForSelectedRow.row]];
}
}
The problem is that the NSLog at the beginning of the method (after the "if") show always the number 0! I tried to implement the method:
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
but the prepare for segue method is executed before this method, so I can't set any @property for "save" the indexPath. How can I solve this problem?
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.
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.
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.
In:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
...you have the 'sender' parameter that lets you know the Cell of the accessory that has been Tapped, so with:
index = [self.tableView indexPathForCell:sender].row;
You can get the index of it in order to use it.
Then you don't need to use the tableView's accessoryButtonTappedForRowWithIndexPath method.
I fix the problem in this way: I connected the UIViewController to the destinationViewController (NOT FROM THE ACCESSORY BUTTON BUT FROM THE UIViewController).
I created a @property in the first View Controller:
@property (nonatomic) NSInteger indexPathRow;
then I modified the code like this:
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Disclosure Tapped----> %i" ,indexPath.row);
self.indexPathRow = indexPath.row;
[self performSegueWithIdentifier:@"IdentityCard" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"IdentityCard"])
{
NSLog(@"%i" ,self.tableView.indexPathForSelectedRow.row);
[segue.destinationViewController showIdentityCardForTeacher:[self.teachers getTeacherInPosition:self.indexPathRow]];
}
}
Now it works!
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