0

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];
}
4

1 回答 1

0

创建一个整数属性(在本例中我称之为 buttonTag),并在 sendMail: 方法中将其值设置为发件人的标记值(注意我将发件人的类型从 id 更改为 UIButton*):

- (IBAction)sendMail:(UIButton *)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Valg" delegate:self cancelButtonTitle:@"Avbryt" destructiveButtonTitle:nil otherButtonTitles:@"Rediger",@"Forhåndsvis", nil];
    destructiveButtonTitle:nil otherButtonTitles:@"Send via epostklient",@"Send via Uni24", nil];
    self.buttonTag = sender.tag;
    [actionSheet showInView:self.view];
}

然后,在 prepareForSegue 中,您可以使用 self.buttonTag 来获取您触摸其辅助视图按钮的单元格的 indexPath.row。

于 2013-07-15T20:11:27.493 回答