2011-06-26 18 views
0

我想要做的是在我的searchResultsTableView調用selectRowAtIndexPath。我在viewWillAppear中這樣做,以便在從另一個視圖控制器返回後選中該行。在這個簡單的例子中,我只是要求它到第2行。發生的是它簡要地選擇第2行,然後選擇消失。有任何想法嗎?試圖調用selectRowAtIndexPath我的searchController.searchResultsTableView

-(void) viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    // just select row 2 as a test 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:0]; 
    [self.searchController.searchResultsTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 
    [self.searchController.searchResultsTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:NO]; 
} 

感謝

回答

2

也許你應該把它在viewDidAppear:,所以它的選擇,一旦認爲是存在的,而不是它的呈現之前。你的控制器的超類可能會取消選擇viewDidAppear上的所有行:(雖然我不確定)。

另外,我覺得你可以在短短一個做這兩條線:

[self.searchController.searchResultsTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionTop]; 
+0

完美!非常感謝你。埃裏克 –

0

viewDidLoadviewDidAppear剛進去,只是使用由EmilioPelaez告訴單行。