Ios UITableView-IndexPath始终首先返回0&;单击两下,将正确的数据传递给view controller
我有一个连接到视图控制器的Ios UITableView-IndexPath始终首先返回0&;单击两下,将正确的数据传递给view controller,ios,iphone,objective-c,uitableview,Ios,Iphone,Objective C,Uitableview,我有一个连接到视图控制器的UITableView。My tableview包含一个位置名称列表,当您选择位置时,它会将有关所选位置的更详细数据传递给视图控制器。我的问题是,无论单击哪一行,我的索引路径在第一次单击/加载时总是返回0。我必须单击每一行两次,以便它传递正确的数据 示例:单击第2行>返回第1行的(0)数据。然后再次单击第2行>正确返回第2行的数据>然后单击第3行>返回第2行的数据>我必须再次单击第3行以获取第3行的数据InExpathSelected在下面的代码中,代码总是0加载 NS
UITableView
。My tableview包含一个位置名称列表,当您选择位置时,它会将有关所选位置的更详细数据传递给视图控制器。我的问题是,无论单击哪一行,我的索引路径在第一次单击/加载时总是返回0。我必须单击每一行两次,以便它传递正确的数据
示例:单击第2行>返回第1行的(0)数据。然后再次单击第2行>正确返回第2行的数据>然后单击第3行>返回第2行的数据>我必须再次单击第3行以获取第3行的数据<代码>InExpathSelected在下面的代码中,代码总是0
加载
NSInteger indexPathSelected;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
indexPathSelected = indexPath.row;
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showLocationDetails"])
{
LocationDetailsViewController *vc = [segue destinationViewController];
vc.location = _locationsArray [indexPathSelected];
}
}
prepareforsgue:sender:
可能在tableView:didSelectRowAtIndexPath:
之前被调用,因此第一次加载是初始化indexPathSelected
,默认值为0,然后每次都传递上一个值
我将去掉tableView:didSelectRowAtIndexPath:
,并将prepareforsgue:sender:
更改如下:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showLocationDetails"])
{
LocationDetailsViewController *vc = [segue destinationViewController];
NSIndexPath selectedIndexPath = [[self tableView] indexPathForSelectedRow];
vc.location = _locationsArray[[selectedIndexPath row]];
}
}
它应该完成与您想要的相同的任务。
prepareForSegue:sender:
可能在tableView:didSelectRowAtIndexPath:
之前被调用,因此第一次加载是使用0作为默认值初始化indexPathSelected
,然后每次传递上一个值
我将去掉tableView:didSelectRowAtIndexPath:
,并将prepareforsgue:sender:
更改如下:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showLocationDetails"])
{
LocationDetailsViewController *vc = [segue destinationViewController];
NSIndexPath selectedIndexPath = [[self tableView] indexPathForSelectedRow];
vc.location = _locationsArray[[selectedIndexPath row]];
}
}
它应该完成与您想要的相同的任务。问题是在didSelectRowAtIndexPath之前调用了prepareforsgue。在使用segues时,通常根本不需要实现didselectrowatinexpath。prepareForSegue:sender:中的sender参数是您选择的单元格,因此您可以从中获取索引路径:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showLocationDetails"])
{
LocationDetailsViewController *vc = [segue destinationViewController];
indexPathSelected = [self.tableView indexPathForCell:sender].row;
vc.location = _locationsArray[indexPathSelected];
}
}
问题是在didselectrowatinexpath之前调用prepareforsgue。在使用segues时,通常根本不需要实现didselectrowatinexpath。prepareForSegue:sender:中的sender参数是您选择的单元格,因此您可以从中获取索引路径:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showLocationDetails"])
{
LocationDetailsViewController *vc = [segue destinationViewController];
indexPathSelected = [self.tableView indexPathForCell:sender].row;
vc.location = _locationsArray[indexPathSelected];
}
}
工作完美。谢谢工作得很好。谢谢工作完美。谢谢工作完美。谢谢