0

我的视图中有一个 tableView 并且一切正常,但是每个自定义单元格 (ToDoListCell) 都有一个文本字段,我允许用户编辑、更新他们的数据。一切都很好,每当尝试滚动到将被键盘隐藏的单元格时,都会出现我的问题。

注意:我按照 Apple 文档的建议使用 NSNotificationCenter 观察者方法调整表的大小,我知道这不是问题。

- (void)scrollToSelectedCell {

        NSIndexPath *currentIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
        while (currentIndexPath.row < self.todos.count) {
           ToDoListCell *cell = (ToDoListCell *)[self.table cellForRowAtIndexPath:currentIndexPath];
                if (cell.titleField.isEditing == YES) {
                      [self.table scrollToRowAtIndexPath:currentIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
                      break;
                }

           currentIndexPath = [NSIndexPath indexPathForItem:currentIndexPath.row + 1 inSection:0];

         }
}

知道为什么它不会滚动到当前不在屏幕上的任何单元格吗?

谢谢,

4

2 回答 2

0

只是猜测,但可能是因为

ToDoListCell *cell = (ToDoListCell *)[self.table cellForRowAtIndexPath:currentIndexPath];

正在返回nil导致cell.titleField.isEditing == YES总是失败。所以滚动命令永远不会被调用。表格视图只保留可见单元格,因此如果您调整了表格视图的大小以使正在编辑的单元格不再可见,您可能会nil回来。在调整表格大小之前,您需要确定正在编辑的单元格的索引路径。

于 2013-08-11T01:01:13.433 回答
0

如果您触发代码以响应通知侦听器,您是从 UI 线程触发通知吗?

除非在主线程上执行,否则 UI 动画将不会执行。

dispatch_async(dispatch_get_main_thread(), ^{
   // send notification
});
于 2016-10-17T02:15:44.143 回答