我的应用程序中有一个带有Autolayout的UITableView.
按下LoadEarlier Button索引[0]后,我需要在TableView中插入一些行,
但我想留在我的TableView的最后一个位置.(像WhatsApp& …那样加载早期).
按下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];