2012-03-29 29 views
4

我注意到scrollToRowAtIndexPath:atScrollPosition:animated:不滾動到當前不在視線中的單元格,所以如果我有100個單元格並且我需要到達70中的那個單元格,該選擇器將無能爲力。scrollToRowAtIndexPath不滾動到非活動/卸載的單元格

有沒有辦法讓這個細胞進入記憶?我已經有單元格的索引路徑...

我需要滾動到我的應用程序中的位置,當用戶想要去那裏。

感謝您的任何想法!

編輯:@dasblinkenlight

- (void)viewDidLoad 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil]; 
} 

- (void)keyboardWillHide 
{ 
    //Load remote cell here then scroll 
    // :(dont know how to load remote cell yet 
} 

- (void)keyboardWillShow 
{ 
    //Load remote cell here then scroll 
    // :(dont know how to load remote cell yet 
    //_cellIndexPath gets assigned on didSelectRowAtIndexPath: 
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:_cellIndexPath.row inSection:_cellIndexPath.section] atScrollPosition:UITableViewScrollPositionBottom animated:YES]; 
} 

EDIT2:

- (void)keyboardWillShow 
{ 
//Load remote cell here then scroll 
    [NSThread detachNewThreadSelector:@selector(keyboardWillShowThreaded) toTarget:self withObject:nil]; 
} 

- (void)keyboardWillShowThreaded 
{ 
    [NSThread sleepForTimeInterval:2.0]; 
    [self performSelectorOnMainThread:@selector(keyboardWillShowMainThread) withObject:nil waitUntilDone:YES]; 
} 

- (void)keyboardWillShowMainThread 
{ 
    //Get the cell 
    //_textFieldThatHasFirstResponder is a subview in the cell 
    //This returns null, probably because the cell is not loaded into memory 
    UITableViewCell *cell = [_textFieldThatHasFirstResponder superview]; 
    NSLog(@"CELL TO SCROLL TO: %@",cell); 
    NSIndexPath *indexPathForCell = [self.tableView indexPathForCell:cell]; 
    [self.tableView scrollToRowAtIndexPath:indexPathForCell atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 
} 
+1

它*應*滾動到可見區域外的單元格。你能說明你是怎麼稱呼它的嗎?你是從哪種方式來打電話的? – dasblinkenlight 2012-03-29 13:38:15

+0

是的,也許你是對的,但我有我的8個細胞可見,當我點擊例如。第二個,它滾動得很好,但是當我點擊第8個單元格的時候,注意到會出現一些細胞,但不可見,那個看不見的單元格也不滾動。 – 2012-03-29 13:45:55

+0

你還可以顯示你調用'scrollToRowAtIndexPath'的部分嗎? – dasblinkenlight 2012-03-29 13:48:58

回答

0

你可以實現一個委託,這樣就可以從你身在何處類調用它,所以這是可以更新的位置

+0

我從UITableView的類中調用它(即。這個類是UITableView的委託) – 2012-03-29 13:47:44

0

「我已經注意到,scrollToRowAtIndexPath:atScrollPosition:animated: 不會滾動細胞是沒有t目前看來,所以如果我有 100個單元,並且我需要到達70的那個單元,那麼對該 選擇器的調用將不會執行任何操作。 「

號是不正確的。我剛纔想用100行的實現代碼如下,下面的代碼工作得很好。

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:78 inSection:0] 
        atScrollPosition:UITableViewScrollPositionNone animated:YES]; 
+0

我現在沒有得到它,滾動只適用於tableview的前5個單元格...我試過所有不同的UITableViewScrollPositions。如果我單擊6+數字單元格鍵盤只是它的前面 – 2012-03-29 13:59:02

+0

也許你的加載時間是一個問題,看看https://discussions.apple.com/thread/2154009?start=0&tstart=0 – Vignesh 2012-03-29 14:00:39

+0

看到我的OP中的編輯2,改變了代碼,以包括一些時間。仍然沒有 – 2012-03-29 14:17:45

0

我不認爲增加睡眠改變任何東西。它你可以檢查你傳遞給scrollToRowAtIndexPath的索引是否有效嗎?我記得自己看到了同樣的問題,但它與不可見的單元格有關,不可能檢索到不可見的單元格(tableView返回nil ),因此其索引路徑爲零,因此滾動失敗。 您可以存儲所有單元格的位置,或者即時計算它的位置,然後通過它到

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated; 

這是我能想象的唯一解決方案。

1

OK,我有這種情況的原因,看,你有:

NSIndexPath *indexPathForCell = [self.tableView indexPathForCell:cell]; // nil here 
[self.tableView scrollToRowAtIndexPath:indexPathForCell atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 

當您返回零一個徹頭徹尾的視細胞發送indexPathForCell:,這樣的tableView不知道在哪裏滾動到。