ios - 使用 UITableViewAutomaticDimension 在顶部插入行时保持 UITableView 偏移

标签 ios objective-c uitableview autolayout

我的应用程序中有一个带有 AutolayoutUITableView。 我需要在按 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 来计算我的单元格的高度。

最佳答案

更新

我找不到使 UITableViewUITableViewAutomaticDimension 保持偏移的解决方案。

我已经从 heightForRowAtIndexPathestimateForRowAtIndexPath 中删除了 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/

相关文章:

ios - 根据 UILabel 的宽度设置背景颜色

ios - 可以 renderInContext : maintain corner radius and shadows?

ios - NSUserDefaults 在 Swift 中使用 NSArray 给出错误

objective-c - 我应该使用哪个 __bridge 免费桥接来将 Objective-C 便捷构造函数转换为 C ref?

ios - 在 UITabBarController 中切换选项卡后 UIRefreshControl 卡住了

ios - 无法从父 ViewController 在 UITableViewCell 中设置变量

iphone - 在 iPad 应用程序中从 UIView 创建 pdf 的问题

objective-c - 了解 Cocoa Drawing 中的点和用户空间,因为它们与屏幕分辨率交互

ios swift 显示/隐藏textView单元格中的元素并清除其空间

ios - UITableView 重新加载部分/重新加载数据 : header view becomes blank