indexPath.row不一致

时间:2014-03-03 18:11:53

标签: ios7 tableview segue

在didSelectRowAtIndexPath中,indexPath.row在选择单元格时显示正确的索引。但是在准备segue时,它始终为0,它始终将第一个对象发送到我的detailView中。为什么prepareForSegue indexPath与didSelectRowAtIndexPath不相同?

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"%i",indexPath.row);
    [self performSegueWithIdentifier:@"showDetailSegue" sender:tableView.indexPathForSelectedRow];
}

 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"showDetailSegue"])
    {

        UITableViewCell *cell = (UITableViewCell*) sender;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        NSString *thisFName = [[_resultsArray objectAtIndex:indexPath.row] valueForKey:@"firstName"];
        //NSLog(@"%i",indexPath.row);
        NSString *thisLName = [[_resultsArray objectAtIndex:indexPath.row] valueForKey:@"lastName"];
        //NSString *fullName = [NSString stringWithFormat:@"%@ %@",thisFName,thisLName];
        NSString *thisPhone = [[_resultsArray objectAtIndex:indexPath.row] valueForKey:@"phone"];
        //NSString *thisLName = [[_resultsArray objectAtIndex:indexPath.row] valueForKey:@"lastName"];
        NSString *thisEmail = [[_resultsArray objectAtIndex:indexPath.row] valueForKey:@"email"];

        JCDetailViewController *controller = (JCDetailViewController *)segue.destinationViewController;
        controller.firstName = thisFName;
        controller.lastName = thisLName;
        controller.phoneString = thisPhone;
        controller.emailString = thisEmail;
        NSLog(@"%@",thisFName);


    }
}

2 个答案:

答案 0 :(得分:1)

Apple生产的所有标准代码都使用以下内容:

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

prepareForSegue:sender:内。

在您的方法中,您希望该单元格是prepareFoeSegue:中的发件人,但您发送此单元格的索引路径

[self performSegueWithIdentifier:@"showDetailSegue" sender:tableView.indexPathForSelectedRow];

因此,您应该可以使用prepareForSegue:中的路径

[[self fetchedResultsController] objectAtIndexPath:sender];

答案 1 :(得分:0)

您的代码不正确。当您致电performSegueWithIdentifier时,您正在为NSIndexPath *参数传递sender。但随后在prepareForSegue中,您sender投了UITableViewCell *

如果您希望prepareForSegue中的单元格传递performSegueWithIdentifier中的单元格;否则,将sender投射到NSIndexPath *