Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 以前UITableView上的indexPathForSelectedRow是否始终返回零?_Iphone_Objective C_Uitableview - Fatal编程技术网

Iphone 以前UITableView上的indexPathForSelectedRow是否始终返回零?

Iphone 以前UITableView上的indexPathForSelectedRow是否始终返回零?,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,我有一个UITableView堆栈,由UINavigationController管理。当我尝试在前面的任何UITableView上调用indexPathForSelectedRow时,我总是得到零: NSEnumerator *e = [[navigationController viewControllers] objectEnumerator]; UITableViewController *controller; while (controller = [e nextObject]) {

我有一个UITableView堆栈,由UINavigationController管理。当我尝试在前面的任何UITableView上调用indexPathForSelectedRow时,我总是得到零:

NSEnumerator *e = [[navigationController viewControllers] objectEnumerator];
UITableViewController *controller;
while (controller = [e nextObject]) {
    NSIndexPath *selectedIndexPath = [[controller tableView] indexPathForSelectedRow]; // always nil
    // ...
}

我在苹果的文档中找不到关于这种行为的任何信息;这是故意的吗?如果是这样的话,有没有其他方法来完成我想做的事情,而不必在didSelectRowAtIndexPath或诸如此类的东西中手动管理冗余的mySelectedIndexPath实例变量?

可以随时卸载
UINavigationController
堆栈中的不可见视图,因此,您不能依赖于不可见视图的状态


很有可能您正在引用的
UITableViewController
已经卸载了它的表视图,在这种情况下,调用
[controller tableView]
将实例化一个新视图。

我在iPad的拆分屏幕视图(左、右细节窗格)中也注意到了这一点,这有点烦人。但是,我发现如果覆盖ViewWillDisplay,则可以在调用[super ViewWillDisplay]之前获取表视图的选定行

如果有这样的代码,indexPathForSelectedRow将为零

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if(![_self.tableView indexPathForSelectedRow])
    {
        NSLog(@"nothing selected, so selecting 1st one");
            // sets the detail pane here. 
    }
}
但是,如果代码是这样的(请注意,视图将显示:在末尾),那么indexPathForSelectedRow将不会为零

- (void)viewWillAppear:(BOOL)animated {
    if(![_self.tableView indexPathForSelectedRow])
    {
        NSLog(@"nothing selected, so selecting 1st one");
                // sets the detail pane here. 
    }
    [super viewWillAppear:animated];
}

这应该行得通。可能navigationController没有引用正确的对象。如何将NavigationConcoller放入推送到堆栈上的视图中;谢谢你的信息。这在任何地方都有记录吗?请参阅UIViewController文档中的“内存管理”部分。这似乎有效的唯一原因是您使用的是UITableViewController,并且清除SelectionInviewMillAppear默认值为“是”。您应该将ClearSelectionInviewMillAppear设置为NO,然后您可以随意访问所选行,而不用担心UITableViewController会为您取消选择。