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

前端之家收集整理的这篇文章主要介绍了iphone – scrollToRowAtIndexPath不滚动到非活动/卸载的单元格前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我注意到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];
}

解决方法@H_404_29@
好的,我有原因,看,你有:

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

当您发送indexPathForCell时:对于视图外单元格,它返回nil,因此tableView不知道滚动到哪里.

猜你在找的Xcode相关文章