【问题标题】:Segue not passing correct indexpath of row always section 0Segue 不传递正确的行的索引路径总是第 0 节
【发布时间】:2014-03-29 04:38:00
【问题描述】:

我使用的是分段的 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]];
}

代码有什么问题?

【问题讨论】:

  • 有多少个部分?也许你的意思是行而不是部分。
  • 我有 4 个部分..总是返回 0。@KudoCC
  • @gagarwal 你能举个例子吗?
  • @gagarwal 请用我的代码编辑

标签: ios objective-c uitableview ios7 uistoryboardsegue


【解决方案1】:

您需要在您的序列中创建 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];


}

【讨论】:

    【解决方案2】:

    是的,Ramdy,是对的。您需要提及行而不是部分。
    你用这个....

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

    【讨论】:

      【解决方案3】:
      - (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]];
      }
      

      请检查您的数组是否具有来自您的部分表格视图的正确数据,否则将 NSDictionary 值添加到数组并将其传递给 detailTableViewController 然后尝试。问题是您没有在此处传递部分的索引路径。希望它对您有所帮助.

      【讨论】:

      • 还是一样。你能再给点样品吗
      • 请在 Skype 中加我。知道我的 id 了吗?
      【解决方案4】:

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

      selectRowAtIndexPath:animated:scrollPosition:
      

      在以下 UITableViewDelegate API 中:

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

      【讨论】:

        【解决方案5】:
        -(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;
        }
        

        【讨论】:

        • 如果我改变行它只通过每个部分的所有第一行。 @Ramdy
        • PassRow 和 PassSection 是什么属性? @Ramdy
        • 必须在detailView中声明为字符串?
        • 它只是 NSString ,在 detailTableViewController 中声明。
        • 来自 prapareForSegue,我们只发送行和部分。只需使用 nslog("%@ %@",passrow,passsection);要打印值,只需检查一下,在此之前这些 passrow , passsection 应该是一个属性并合成。
        【解决方案6】:

        最接近该问题的答案是 Nitin Gohel。但是如果你在Storyboard中将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 名称。

        【讨论】:

        • 感谢您的回答。仍然相同的问题始终传递每个部分的第一个单元格行值
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-05
        • 2020-03-19
        • 1970-01-01
        • 2018-07-17
        • 2012-02-23
        • 2016-04-16
        相关资源
        最近更新 更多