scrollToRowAtIndexPath不滚动到非活动/卸载的单元格

时间:2012-03-29 13:32:58

标签: iphone objective-c xcode ios5

我注意到scrollToRowAtIndexPath:atScrollPosition:animated:没有滚动到当前不在视图中的单元格,所以如果我有100个单元格并且我需要到达70处的那个单元格,那么对该选择器的调用将会执行什么都没有。

有没有办法让那个细胞进入记忆?我已经有了单元格的索引路径......

当用户想要去那里时,我需要滚动到我的应用中的那个位置。

感谢您的任何想法!

编辑:@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];
}

3 个答案:

答案 0 :(得分:1)

好的,我找到了原因,看,你有:

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

当您为视图外单元发送indexPathForCell:时,它返回nil,因此tableView不知道在哪里滚动。

答案 1 :(得分:0)

你可以实现一个委托,这样你就可以从你所在的类中调用它,这样就可以更新位置

答案 2 :(得分:0)

  

“我注意到scrollToRowAtIndexPath:atScrollPosition:animated:   不滚动到当前不在视图中的单元格,所以如果我有   100个单元格,我需要到达70处的那个,这是对它的调用   选择器什么都不做。 “

没有。这不是真的。我刚尝试了100行的tableview。以下代码效果很好。

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