Ios 触摸显示操作表的附件按钮时,在tableview中获取正确的行

Ios 触摸显示操作表的附件按钮时,在tableview中获取正确的行,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个动态单元格的表视图 当用户按下单元格中的自定义按钮(附件按钮)时,他们会得到两个选项:预览或编辑(或挪威语的Forhåndsvis/Rediger)。因此,用户可以通过选择打开2个不同的视图(在故事板中) 问题是如何获得正确的indexpath.row 在my-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 我使用了UIButton的一个子类

我有一个动态单元格的表视图

当用户按下单元格中的自定义按钮(附件按钮)时,他们会得到两个选项:预览或编辑(或挪威语的Forhåndsvis/Rediger)。因此,用户可以通过选择打开2个不同的视图(在故事板中)

问题是如何获得正确的indexpath.row

在my
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

我使用了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;
当用户按下此按钮时,将触发iAction方法:

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

以及操作表的处理程序:

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

这个小程序调用
prepareforsgue
方法,该方法试图将正确的数据传递给正确的视图。它的工作方式与我想要的一样,但我无法传递正确的数据,因为它总是从第一行发送数据

我如何尝试查找用户触摸过的正确行的示例:

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

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


然后,在prepareForSegue中,您可以使用self.buttonTag获取您所触碰的附件视图按钮所在单元格的indexPath.row。

非常简单。非常感谢。
- (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];
}