UITableView的IndexPath不起作用

时间:2011-11-16 06:09:48

标签: iphone objective-c cocoa-touch didselectrowatindexpath

我正在尝试确定indexPath的选定行的UITableView,以便我可以相应地传递相关数据。现在,无论我选择哪个单元格,它都会记录0。有什么想法吗? (是的,表格的IBOutlet已正确连接)。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"patientChart"])
    {
        UITabBarController *tabBar = (UITabBarController *)segue.destinationViewController;
        PatientChartViewController *vc = [tabBar.childViewControllers objectAtIndex:0];
        NSIndexPath *path = [self.appointmentTableView indexPathForSelectedRow];
        vc.appointmentDictionary = [self.appointmentArray objectAtIndex:path.row];
    }
}

2 个答案:

答案 0 :(得分:3)

如果选择了行,方法indexPathForSelectedRow将返回所选行的索引,否则返回0。

因此,例如,如果选择了cell并稍后取消选择,那么您将无法通过调用index path获取[tableView indexPathForSelectedRow];,因为此时没有选择任何一个单元格。 / p>

答案 1 :(得分:1)

如果使用iOS5,解决方法是在.h中声明一个实例,并设置要复制的属性,如下所示。

NSIndexPath *path;
@property (nonatomic, copy) NSIndexPath *path;

将indexpath值分配给实例,即cellForRowAtIndexPath方法中的路径。

我尝试在iOS5上执行此操作时遇到此问题,并按上述方法解决此问题。希望这有助于你。