问题描述
我有一个带有项目的_TableView
,我想设置自动刷新,并且我不希望它在刷新时滚动,可以说用户向下滚动了2页,并且刷新触发了->,所以我想将刷新的内容放在表格的顶部,而不会影响用户的滚动
I have a _TableView
with items , and I want to set automatic refresh,and I don't want it to scroll on refresh , lets say user scrolled 2 pages down , and the refresh trigered -> so I want to put the refreshed content to the top of the table without interupting user's scrolling
假设用户位于第18行现在_dataSource
刷新了,所以它取了4个项目,所以我希望用户继续使用他以前的项目.
Assume user was on row 18and now the _dataSource
is refreshed so it fetched lets say 4 items , so I want user to stay on the item he was.
实现它的最佳方法是什么?
What would be the best approach to achieve it ??
推荐答案
我正在显示是否仅添加一行.您可以将其扩展为多行.
I am showing if only one row is being added. You can extend it to multiple rows.
// dataArray is your data Source object
[dataArray insertObject:name atIndex:0];
CGPoint contentOffset = self.tableView.contentOffset;
contentOffset.y += [self tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
[self.tableView reloadData];
[self.tableView setContentOffset:contentOffset];
但是要执行此操作,您需要定义- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
方法.否则,如果常量不变,您可以直接给您的tableview行高.
But for this to work you need to have defined - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
the method. Or else, you can directly give your tableview row height if it is constant.
这篇关于UITableView刷新而不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!