滚动和插入新行后的怪异UITableView行为 [英] Weird UITableView behavior after scrolling and inserting new row

查看:100
本文介绍了滚动和插入新行后的怪异UITableView行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真的使我发疯.

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

I have a table view that displays a sorted list of customers. Users can add a new customer, thus I have to add a new row to the table view (by updating the data model and calling insertRowsAtIndexPaths:withRowAnimation:). However, since the table view is sorted, this insert can occur off-screen, which is not very nice wrt. user experience. So my idea was to scroll the table view to the index path where the insert will occur before actually inserting the row:

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

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

This code actually works as expected: The table view scrolls and the new row is inserted correctly.

但是,插入后,某些表视图单元格将变得无响应" ,即.他们在滑动时不会显示删除按钮,并且在选中时甚至不会突出显示.由于这确实很奇怪并且很难解释,所以让我尝试说明一下.

However, after the insert some table view cells become "irresponsive", ie. they won't display the delete button upon swipe and don't even highlight when selected. Since this is really weird and kinda hard to explain, let me try to illustrate it.

初始情况:显示第1至5行:

Initial situation: Rows 1 - 5 are displayed:

--------- 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之后,将不再如上所述响应用户交互.

Now, after the insert rows 1 - 5 will not respond to user interaction as described above.

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

Anybody ever experienced this kind of problem? Ideas? Are there other approaches to make sure that the insertion of new rows is visible for the user?

更新:刚刚在设备上进行了测试,结果发现此问题仅在模拟器上发生!这样可以减轻问题的困扰,但如果有人遇到这种情况,我仍然很感兴趣.

Update: Just tested on the device, and it turns out that this problem only occurs on the simulator! This makes the issue less unpleasant, but I'd still be interested if someone else has experienced this behavior.

推荐答案

我最近在Simulator上也有类似的经历-我正在处理一个应用程序,该应用程序从Core Data驱动多个表视图,并且还提供了一堆UIAlertViews和UIActionSheets遍及该应用程序.

I had some similar experiences with the Simulator recently - I'm dealing with an application driving several table views from Core Data, and it also presents a bunch of UIAlertViews and UIActionSheets at various points through the application.

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

I've noticed that the Simulator doesn't respond well to touch events right after a UI action (table cell insert, action sheet display, etc.) occurs. It usually helps for me to either

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

您的里程可能会有所不同.在测试过程中可能会很痛苦,但是只要一切都能在设备上正常运行,就可以了.

Your mileage may vary. It may be a pain during testing, but as long as everything works properly on the device, you should be fine.

这篇关于滚动和插入新行后的怪异UITableView行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆