iphone - scrollToRowAtIndexPath 不滚动到非事件/卸载的单元格

标签 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];
}

编辑2:

- (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];
}

最佳答案

好的,我知道了原因,你看,你有:

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

当您为 View 外的单元格发送 indexPathForCell: 时,它返回 nil,因此 tableView 不知道要滚动到哪里。

关于iphone - scrollToRowAtIndexPath 不滚动到非事件/卸载的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9926424/

相关文章:

ios - 将虚拟对象添加到项目

iphone - NSSortDescriptor,获取按日期排序的最新 5 条记录

ios - UILabel 背景图像在 iPhone 5 及以下版本上消失

iOS应用程序中的C++算法

ios - 将 ManagedObjectID 存储在 NSDictionary 中

ios - 针对特定情况优化 iOS 应用程序

Xcode:在没有开发者帐户的情况下启用应用程序组?

iphone - uitableviewcell中的两个部分

iphone - 如何使用 NSURLRequest 访问 WebDynpro 页面

ios - 当 UITableView 完成请求数据时得到通知?