indexPathForSelectedRow返回nil [英] indexPathForSelectedRow returning nil

查看:330
本文介绍了indexPathForSelectedRow返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有segue的UITableViewController我正在尝试获取当前选定的行:

I have a UITableViewController with a segue where I'm trying to get the currently selected row:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([[segue identifier] isEqualToString:@"EditFilterElementSegue"]){
        // Get the element that was clicked and pass it to the view controller for display
        NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
        Element *element = [fetchedResultsController objectAtIndexPath:selectedIndexPath];
        FilterElementTableViewController *vc = segue.destinationViewController;
        vc.filter = filter;
        vc.element = element;
    }
}

问题是indexPathForSelectedRow返回nil。文档说如果索引路径无效,则返回nil。如果我添加:

The problem is indexPathForSelectedRow is returning nil. The docs say nil is returned "if the index path is invalid". If I add:

        [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:0];
        selectedIndexPath = [self.tableView indexPathForSelectedRow];

selectedIndexPath有效。所以,我现在假设行以某种方式取消选择。任何人都可以告诉我如何判断是否是这种情况以及可能导致它的原因?

selectedIndexPath is valid. So, I'm currently assuming the row is somehow getting unselected. Can anyone tell me how to figure out if this is the case and what might be causing it?

此代码工作正常,直到我转换为使用NSFetchedResultsController(否则正在工作) )。我还在几个其他表视图控制器中使用indexPathForSelectedRow,它可以正常工作(其中一个也使用NSFetchedResultsController)。

This code worked fine until I converted to using NSFetchedResultsController (which otherwise is working). I also use indexPathForSelectedRow in several other table view controllers where it works fine (one of which also uses NSFetchedResultsController).

我也试过打破didSelectRowAtIndexPath,但是同样的事情发生在那里。 (预计因为它是在prepareForSegue之后调用的。)

I've also tried breaking in didSelectRowAtIndexPath, but the same thing happens there. (expected since it's called after prepareForSegue.)

推荐答案

好的,底线是故事板中的场景搞砸了。

Ok, the bottom line is the scene in storyboard was messed up.

为了解决这个问题,我删除了segue并创建了一个全新的空UITableViewController类,并且只添加了足够的代码,因此我可以单击一行并查看indexPathForSelectedRow。它仍然是零。

To figure it out I removed the segue and created a brand new empty UITableViewController class and only added enough code so I could click on a row and look at indexPathForSelectedRow. It was still nil.

然后我在故事板中创建了一个新的UITableViewController并替换了现有的UITableViewController。现在indexPathForSelectedRow工作了。

I then created a new UITableViewController in storyboard and replaced the existing one. Now indexPathForSelectedRow worked.

我复制了所有的类代码,并且原型单元格已经完成了,它仍然有效!

I copied all the class code over, and the prototype cells over, and it still worked!

出于好奇,我查看了两个场景的故事板来源,并注意到无法工作的场景比新场景长得多。看一下故事板大纲,这显然是为什么,并且令人惊讶的是它起作用了。而且,现在我记得切割时遇到了麻烦。粘贴原型细胞。我猜我应该更频繁地看一下大纲模式。请参见下面的屏幕截图(视觉场景看起来相同):

Out of curiosity I looked at the storyboard source for the two scenes and noticed that the scene that wasn't working was much longer than the new one. Looking at the storyboard outline it was obvious why, and surprising it worked at all. And, now I remember having trouble cutting & pasting a prototype cell. Guess I should look at the outline mode more often. See the screen shot below (the visual scenes look the same):

这篇关于indexPathForSelectedRow返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆