传递indexpath以从uitablerowaction按钮执行SUNwithidentifier

时间:2014-12-02 03:53:10

标签: ios xcode segue

这是我的delegate方法:

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath     *)indexPath
{
UITableViewRowAction *button = [
                                UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"More" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
{
    [self performSegueWithIdentifier:@"AreaDescriptionSegue" sender:indexPath];
    NSLog(@"More button tapped");
}
                                ];
button.backgroundColor = [UIColor grayColor]; //arbitrary color
return @[button]; //array with all the buttons you want. 1,2,3, etc...
}

这是我的prepareforsegue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"AreaDescriptionSegue"]) {
    NSLog(@"segue from Areas screen");
    NSIndexPath *newIndexPath = [self.listTableView indexPathForCell:sender];
    AreaDescriptionController *vc = (AreaDescriptionController*)[segue destinationViewController];
    vc.area = _feedItems[newIndexPath.row];

}
}

对象传递给destinationViewController但它始终是第一个对象项而不是正确的indexPath

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

您将indexPath作为发送方传递(在performSegue中),因此不需要使用此行获取indexPath(事实上它不起作用,因为“sender”不是单元格),

NSIndexPath *newIndexPath = [self.listTableView indexPathForCell:sender]

你应该这样做,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(NSIndexPath *)sender {
    if ([[segue identifier] isEqualToString:@"AreaDescriptionSegue"]) {
        NSLog(@"segue from Areas screen");
        AreaDescriptionController *vc = (AreaDescriptionController*)[segue destinationViewController];
        vc.area = _feedItems[sender.row];

    }
}

你总是得到第一行的原因是newIndexPath将为nil,newIndexPath.row的计算结果为0。

答案 1 :(得分:0)

在这一行中,

[self performSegueWithIdentifier:@"AreaDescriptionSegue" sender:indexPath];

您已经在发送索引路径了。然后,

NSIndexPath *newIndexPath = [self.listTableView indexPathForCell:sender]; 

您将发件人视为Cell。实际上发件人是indexPath。您应该直接将sender用作indexPath。