认为没有传递正确的行的索引路径总是第0节

时间:2014-03-29 04:38:00

标签: ios objective-c uitableview ios7 uistoryboardsegue

我使用了分区tableView。如果我点击tableview,它总是将indexpath 0传递给详细视图控制器。如果我点击第二行但它的indexpath传递总是传递0。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    if ([segue.identifier isEqualToString:@"toNext"]) {
    detailTableViewController *detailVC = [segue destinationViewController];
    [detailVC setkMessageDict:(NSDictionary*)[nArray objectAtIndex:[self.mytableView indexPathForSelectedRow].section]];
}

代码有什么问题?

6 个答案:

答案 0 :(得分:2)

您需要在您的seque中创建 index-path 对象,如: -

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [self performSegueWithIdentifier:@"toNext" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

    NSIndexPath *indexPath = [self.tableview indexPathForSelectedRow];
    NSLog(@" indexPath row %d",indexPath.row);
    NSLog(@" indexPath row %d",indexPath.section);
    detailTableViewController *detailVC = [segue destinationViewController];
    [detailVC setJobDetailDict:(NSDictionary*)[nArray objectAtIndex:indexPath.row];


}

答案 1 :(得分:1)

是的Ramdy,是对的你需要提一下行而不是一节 你用这个....

[detailVC setJobDetailDict:(NSDictionary*)[nArray objectAtIndex:[self.mytableView indexPathForSelectedRow].row]];

答案 2 :(得分:0)

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * currentRow = [NSString stringWithFormat:@"%ld",(long)indexPath.row];  // Declare this row variable in .h file
    NSString * currentSection = [NSString stringWithFormat:@"%ld",(long)indexPath.section];  // Declare this section variable in .h file
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    if ([segue.identifier isEqualToString:@"toNext"]) {
    detailTableViewController *detailVC = [segue destinationViewController];    
    detailVC.PassRow = currentRow;
    detailVC.PassSection = currentSection;
}

答案 3 :(得分:0)

尝试使用API​​更新选定的行indexPath:

selectRowAtIndexPath:animated:scrollPosition:

在以下UITableViewDelegate API中:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

答案 4 :(得分:0)

问题的最接近的答案是 Nitin Gohel 。但是,如果您已将UITableViewCell故事板中的detailTableViewController相关联,则其方法也错误。

在这种情况下,prepareForSegue方法提供了正确的UITableViewCell作为存储在sender参数中的参数。这完全由UITableView处理。所以正确的代码是:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([sender isKindOfClass:[UITableViewCell class]] && [segue.identifier isEqualToString:@"toNext"])
    {
        NSIndexPath *indexPath = [self.mytableView indexPathForCell:sender];

        detailTableViewController *detailVC = [segue destinationViewController];
        [detailVC setJobDetailDict:(NSDictionary*)[nArray objectAtIndex:indexPath.row];
    }
}

不要忘记检查正确的segue名称。

答案 5 :(得分:0)

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    if ([segue.identifier isEqualToString:@"toNext"]) {
    NSIndexPath *selectedIndex= [self.mytableView indexPathForSelectedRow];
    NSDictionary *myDic=(NSDictionary*)[nArray objectAtIndex:indexpath.section];
    detailTableViewController *detailVC = [segue destinationViewController];
    [detailVC setkMessageDict:(NSDictionary*)[[myDic objectForKey:@"locations"] objectAtIndex:selectedIndex.row]];
}

请检查您的数组是否包含来自section tableview的正确数据,否则将NSDictionary值添加到数组并将其传递给detailTableViewController然后尝试。问题是您没有通过此处的索引路径.Hope它对你有帮助。