2012-01-31 122 views
16

我有一個賽格瑞在那裏我試圖讓當前所選行一個UITableViewController:indexPathForSelectedRow返回nil

-(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。如果我添加:

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

selectedIndexPath有效。所以,我目前假設該行以某種方式被取消選中。任何人都可以告訴我如何確定是否是這種情況以及可能導致它的原因?

此代碼工作正常,直到我轉換爲使用NSFetchedResultsController(否則正在工作)。我也在其他幾個表視圖控制器中使用indexPathForSelectedRow工作正常(其中之一也使用NSFetchedResultsController)。

我也試過打破didSelectRowAtIndexPath,但同樣的事情發生在那裏。 (預料的,因爲它prepareForSegue後調用。)

回答

1

好吧,底線是故事板中的場景被搞砸了。

爲了弄清楚我刪除了segue並創建了一個全新的空UITableViewController類,並且只添加了足夠的代碼,因此我可以單擊一行並查看indexPathForSelectedRow。它仍然是零。

然後,我在故事板中創建了一個新的UITableViewController,並替換了現有的一個。現在indexPathForSelectedRow工作。

我把所有的類代碼都複製過來了,原型單元結束了,它仍然有效!

出於好奇,我看了兩個場景的故事板源,並注意到不工作的場景比新的場景長得多。看看故事板的輪廓,很明顯爲什麼,而且令人驚訝的是它的工作。而且,現在我記得在粘貼原型單元格時遇到問題。猜猜我應該更頻繁地看大綱模式。請參見下面的屏幕截圖(視覺場景看起來都一樣):

Storyboard screen shot

22

一個可能的原因是,你在的UITableViewDelegate方法之一書面的東西 -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
} 

這是一個常見的做法是deselect the row剛剛被選中。

+8

對我來說,我是取消了我的排。 「衛生署! – Baub 2012-04-26 23:36:17

9

可以肯定,你是不是要求得到選中行的索引路徑之前deselectRowAtIndexPath方法。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; //add this line after setting indexPath 
} 
+0

reloadData也可能導致取消行 – foOg 2016-04-05 09:48:36

0

我也有這個問題,當我更換一個UITableViewController類代碼中UIViewController類,並沒有更新的故事板即我沒有更換的UITableViewController佔位符故事板,用一個UIViewController故事板。

2

好的在我的情況下,我通過show segue在故事板上從UIButton調用。

和刪除以下行:下面兩行

NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];

工作對我來說:

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];

希望能與我的情況 「從UIButton的細胞叫賽格瑞」 的作品爲他人也。

+0

這是完美的,我正在做類似的事情。我不得不重新加載特定的單元格,但由於該部分在滾動時發生更改,我無法再更新。這裏是我與你的答案一起使用NSArray * indexPathArray = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPathSectionRow.row inSection:indexPathSectionRow.section]]; [self.nationalTableView reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationFade]; – 2018-02-03 04:02:54

12

某些可能會幫助他人的東西 - 如果您在桌面視圖上調用reloadData(),則會詆譭indexPathForSelectedRow

+1

這個令人驚訝的簡單發現剛剛救了我幾個小時;) – 2017-01-06 18:46:14

+0

感謝兄弟!!!它真的有幫助 – SamuelChan 2017-05-23 11:59:18