UITableView 插入行而不滚动 [英] UITableView insert rows without scrolling

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

问题描述

我有一个从 Web 服务中提取的数据列表.我刷新了数据,我想在当前数据上方的表视图中插入数据,但我想在表视图中保留我当前的滚动位置.

I have a list of data that I'm pulling from a web service. I refresh the data and I want to insert the data in the table view above the current data, but I want to keep my current scroll position in the tableview.

现在我通过在当前部分上方插入一个部分来完成此操作,但它实际上是插入、向上滚动,然后我必须手动向下滚动.在此之前,我尝试在表格上禁用滚动,但这也不起作用.

Right now I accomplish this by inserting a section above my current section, but it actually inserts, scrolls up, and then I have to manually scroll down. I tried disabling scrolling on the table before this, but that didn't work either.

这看起来不连贯,而且看起来很笨拙.有什么更好的方法可以做到这一点?

This looks choppy and seems hacky. What is a better way to do this?

[tableView beginUpdates];

[tableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];

[tableView endUpdates];

NSUInteger iContentOffset = 200; //height of inserted rows

[tableView setContentOffset:CGPointMake(0, iContentOffset)];

推荐答案

我发现获得所需行为的最佳方法是根本不为插入设置动画.动画导致了断断续续.

The best way I found to get my desired behavior is to not animate the insertion at all. The animations were causing the choppyness.

相反,我打电话给:

[tableView reloadData];

// set the content offset to the height of inserted rows 
// (2 rows * 44 points = 88 in this example)
[tableView setContentOffset:CGPointMake(0, 88)]; 

这使得重新加载与内容偏移量变化同时出现.

This makes the reload appear at the same time as the content offset change.

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

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