iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 使用UITableViewAutomaticDimension在顶部插入行时保持UITableView偏移大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的应用程序中有一个带有Autolayout的UITableView.
按下LoadEarlier Button索引[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);

如果我从我的代表&删除AutomaticDimension ….它在静态的细胞高度上完美地工作,但是我需要AutomaticDimension来计算我的细胞的高度.

解决方法

更新

我找不到使用UITableViewAutomaticDimension保持UITableView偏移的解决方案.

我从heightForRowATindexPath&中删除了UITableViewAutomaticDimension. estimateForRowATindexPath和保持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偏移全部内容,希望文章能够帮你解决ios – 使用UITableViewAutomaticDimension在顶部插入行时保持UITableView偏移所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。