我使用了分区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]];
}
代码有什么问题?
答案 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它对你有帮助。