Reputation: 1144
I have an UITableView
with Autolayout
in my app.
I need insert some rows to my TableView after press LoadEarlier
Button to index[0],
but i want to stay on my last position of my TableView.(Load Earlier Like WhatsApp & ...).
My problem is after setting contentOffset
the posion of my TableVoew isn't correct.
i check this link but this the problem isn't like my problem but i think the answer help us. UITableViewAutomaticDimension - Cells move when table is reloaded and this link : Keep uitableview static when inserting rows at the top
i do Like this :
// 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);
if i remove AutomaticDimension from my Delegate & .... it's work perfectly on static Height of Cell ,but i need AutomaticDimension to calculate height of my cell.
Upvotes: 4
Views: 1246
Reputation: 1
Simple and effective trick:
var upsideDown: Bool = true
// swap table view vertically
tableView.transform = upsideDown
? CGAffineTransform(scaleX: 1, y: -1)
: .identity
// swap cell in cellForRowAt method
cell.transform = upsideDown
? CGAffineTransform(scaleX: 1, y: -1)
: .identity
Insert rows as usual, the rows will appear on the top of tableview.
This works fine with UITableView.automaticDimension and scrolling stays smooth.
The first rows will be stick to the bottom of tableview though but you can fix it with extra coding.
Upvotes: 0
Reputation: 1144
Update
i can't find a solution for keep UITableView
offset with UITableViewAutomaticDimension
.
i have removed UITableViewAutomaticDimension
from heightForRowAtIndexPath
& estimateForRowAtIndexPath
and for keep 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];
Upvotes: 0