I have a tableview with dynamic cells.
When the user press a custom button (accessory button) in the cell, they get 2 options: Preview or edit (or Forhåndsvis/Rediger in norwegian). So it is possible to open 2 different views (in storyboard) by the choose from the user.
The question is how to get the correct indexpath.row?
In my - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
I have used a subclass of UIButton:
// create a UIButton (UIButtonTypeCustom)
contactAddButtonType = [UIButton buttonWithType:UIButtonTypeCustom];
contactAddButtonType.frame = CGRectMake(250.0, 8.0, 25.0, 25.0);
[contactAddButtonType setTitle:@"Options" forState:UIControlStateNormal];
[contactAddButtonType setBackgroundImage:[[UIImage imageNamed:@"211-action.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
contactAddButtonType.backgroundColor = [UIColor clearColor];
[contactAddButtonType addTarget:self action:@selector(sendMail:) forControlEvents:UIControlEventTouchUpInside];
contactAddButtonType.tag = indexPath.row;
// Add a custom accessibility label to the button because it has no associated text.
[contactAddButtonType setAccessibilityLabel:NSLocalizedString(@"AddContactButton", @"")];
//contactAddButtonType.tag = kViewTag; // tag this view for later so we can remove it from recycled table cells
[cell.contentView addSubview:contactAddButtonType];
cell.accessoryView = contactAddButtonType;
When the user press this button, it fire an IBAction methode:
- (IBAction)sendMail:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"Valg" delegate:self cancelButtonTitle:@"Avbryt" destructiveButtonTitle:nil otherButtonTitles:@"Rediger",@"Forhåndsvis", nil];
//initWithTitle:@"Send epost" delegate:self cancelButtonTitle:@"Avbryt" destructiveButtonTitle:nil otherButtonTitles:@"Send via epostklient",@"Send via Uni24", nil];
[actionSheet showInView:self.view];
}
And a handler for the actionSheet:
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [actionSheet cancelButtonIndex]) {
if (buttonIndex == 0) {
[self performSegueWithIdentifier:@"rediger" sender:self];
}
if (buttonIndex ==1) {
[self performSegueWithIdentifier:@"forhåndsvis" sender:self];
}
}
}
This little cracker calls for the prepareForSegue
method who tries to pass the correct data to the correct view. It works like I want, but I cannot pass the correct data, since it always send the data from the first row.
Example of how I try to find the correct row the user has touched:
if ([segue.identifier isEqualToString:@"forhåndsvis"])
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
NSDictionary *dict = [items objectAtIndex:indexPath.row];
NSLog(@"Setting PersonDetailTVC as a delegate of PersonRoleTVC");
UNIRunReportController *vc = (UNIRunReportController *)[segue destinationViewController];
NSString *ordrenr = [dict objectForKey:@"ID"];
[vc setRapportID:@"10"];
[vc setOrdrenr:ordrenr];
//[vc setIsExcisting:YES];
}