滚动和插入新行后奇怪的UITableView行为

时间:2009-07-12 14:30:11

标签: iphone uitableview insert scroll

这真让我疯狂。

我有一个表格视图,显示已排序的客户列表。用户可以添加新客户,因此我必须向表视图添加新行(通过更新数据模型并调用insertRowsAtIndexPaths:withRowAnimation:)。但是,由于表视图已排序,因此此插入可以在屏幕外进行,这不是很好。用户体验。所以我的想法是在实际插入行之前将表视图滚动到插入将发生的索引路径:

- (void)finishedSavingNewCustomer:(Customer*)customer atIndex:(NSInteger)index {
    // NOTE: self.customers (which is the model for the table view used in all datasource
    // methods) has already been updated at this point, ie. it already contains the new customer

    // scroll the table view so that the insert position is on-screen
    NSInteger scrollRow = (index < ([self.customers count] - 1) ? index : [self.customers count] - 2);
    NSIndexPath* scrollIndexPath = [NSIndexPath indexPathForRow:scrollRow inSection:0];
    [self.tableView scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionNone animated:NO];

    // insert the new row
    NSArray* indexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:index inSection:0]];
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}

此代码实际上按预期工作:表格视图滚动并正确插入新行。

然而,插入后一些表格视图细胞变得“反应迟钝”,即。它们不会在滑动时显示删除按钮,甚至在选中时也不会突出显示。由于这真的很奇怪,有点难以解释,让我试着说明一下。

初始情况:显示行1 - 5:

--------- screen top --------
Row 1
Row 2
Row 3
Row 4
Row 5
------- screen bottom -------
Row 6
Row 7
Row 8 
Row 9

滚动和插入后的情况:

Row 1
Row 2
Row 3
Row 4
Row 5
--------- screen top --------
Row 6
Row 7
Row 7a  << newly inserted
Row 8 
Row 9
------- screen bottom -------

现在,在插入行1-5之后将不响应用户交互,如上所述。

有人遇到过这种问题吗?想法?是否有其他方法可以确保用户可以看到新行的插入?


更新:刚刚在设备上进行了测试,结果发现这个问题只发生在模拟器上!这使问题不那么令人不愉快,但如果其他人遇到这种行为,我仍然会感兴趣。

1 个答案:

答案 0 :(得分:1)

我最近在Simulator上有过类似的经验 - 我正在处理一个从Core Data驱动多个表视图的应用程序,它还通过应用程序在各个点提供了一堆UIAlertViews和UIActionSheets。

我注意到,在UI动作(表格单元格插入,操作表格显示等)发生后,模拟器对触摸事件的响应不佳。这通常对我有帮助

  • 切换到另一个窗口,然后返回模拟器或
  • 单击并将鼠标拖动到模拟器的某些非活动区域,然后再次尝试我想要的操作

您的里程可能会有所不同。在测试过程中可能会很痛苦,但只要设备上的所有设备都正常工作,您就可以了。

相关问题