我的应用程序中有一个带有 Autolayout
的 UITableView
。
我需要在按 LoadEarlier
按钮索引 [0] 后向我的 TableView 插入一些行,
但我想留在我的 TableView 的最后位置。(像 WhatsApp 和...一样提前加载)。
我的问题是在设置 contentOffset
后,我的 TableVoew 的位置不正确。
我检查了这个链接,但这个问题不像我的问题,但我认为答案对我们有帮助。 UITableViewAutomaticDimension - Cells move when table is reloaded 这个链接: Keep uitableview static when inserting rows at the top
我是这样的:
// in ViewDidLoad
self.myTableView.estimatedRowHeight = 100;
self.myTableView.rowHeight = UITableViewAutomaticDimension;
//implemention of LoadEalier Method
CGFloat oldTableViewHeight = self.myTableView.contentSize.height;
for (int i = temp ; i < temp +26; i++) {
myObject * tempObject = [[myObject alloc]init];
tempObject.name = [NSString stringWithFormat:@"Obj : %d",i];
tempObject.uID = [[NSUUID UUID]UUIDString];
[_dataArray insertObject:tempObject atIndex:0];
}
[self.myTableView reloadData];
CGFloat newTableViewHeight = self.myTableView.contentSize.height;
self.myTableView.contentOffset = CGPointMake(0, self.myTableView.contentSize.height - oldTableViewHeight);
如果我从我的 Delegate 中删除 AutomaticDimension & .... 它在单元格的静态高度上完美工作,但我需要 AutomaticDimension 来计算我的单元格的高度。
最佳答案
更新
我找不到使 UITableView
与 UITableViewAutomaticDimension
保持偏移的解决方案。
我已经从 heightForRowAtIndexPath
和 estimateForRowAtIndexPath
中删除了 UITableViewAutomaticDimension
并保留 UITableView
:
CGFloat oldTableViewHeight = self.tableView.contentSize.height; // we keep current content offSet for revert to this position after Reload Table
// Update your Data
[self.tableView reloadData];
CGFloat finalYPostioton = self.tableView.contentSize.height - oldTableViewHeight - numberOfYourSection * sectionHeight;
finalYPostioton = finalYPostioton - spaceFromTopCell; // we need this for showing a little of top cell from our index. spaceFromTopCell -->10
[self.tableView setContentOffset:CGPointMake(0,finalYPostioton) animated:NO];
关于ios - 使用 UITableViewAutomaticDimension 在顶部插入行时保持 UITableView 偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31399188/