Swift Start Tableview from Bottom (Reverse Tableview)

Load tableview from bottom, scroll up (reverse tableview) (iOS)

The best way was to flip the tableView and it's cells. This way, if a cell were to change size (due to things like asynchronous downloading), you will still load from the bottom of the tableview than at an offset.

//In ViewDidLoad
conversationTableView.transform = CGAffineTransform(rotationAngle: -(CGFloat)(Double.pi));

//In cellForRowAtIndexPath
cell.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi));

Also: If you have a headerView, simply set it to the tableView.tableFooterView:

var tableHeaderView = UIView(frame: headerFrame)
tableHeaderView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi));
conversationTableView.tableFooterView = tableHeaderView

And if you have a footerView and headerView, just set the header to the footer, and the footer to the header.

Edit:
If you want to flip the scroll indicator:

conversationTableView.scrollIndicatorInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, conversationTableView.bounds.size.width - 8.0)

swift start tableView from bottom (reverse tableView)

Just scroll to the end and do the same whenever you insert a cell.

tableView.scrollToRowAtIndexPath(bottomIndexPath, atScrollPosition: .Bottom,
animated: true)

How to populate UITableView from the bottom upwards?

To populate UITableView from the bottom:

- (void)updateTableContentInset {
NSInteger numRows = [self.tableView numberOfRowsInSection:0];
CGFloat contentInsetTop = self.tableView.bounds.size.height;
for (NSInteger i = 0; i < numRows; i++) {
contentInsetTop -= [self tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
if (contentInsetTop <= 0) {
contentInsetTop = 0;
break;
}
}
self.tableView.contentInset = UIEdgeInsetsMake(contentInsetTop, 0, 0, 0);
}

To reverse the order of elements:

dataSourceArray = dataSourceArray.reverseObjectEnumerator.allObjects;

Swift 4.2/5 version:

func updateTableContentInset() {
let numRows = self.tableView.numberOfRows(inSection: 0)
var contentInsetTop = self.tableView.bounds.size.height
for i in 0..<numRows {
let rowRect = self.tableView.rectForRow(at: IndexPath(item: i, section: 0))
contentInsetTop -= rowRect.size.height
if contentInsetTop <= 0 {
contentInsetTop = 0
break
}
}
self.tableView.contentInset = UIEdgeInsets(top: contentInsetTop,left: 0,bottom: 0,right: 0)
}

Swift 3/4.0 version:

self.tableView.contentInset = UIEdgeInsetsMake(contentInsetTop, 0, 0, 0)

Load UITableView from the bottom

You can avoid the call from viewDidLoad because scrolling from within viewDidAppear makes that first call redundant. viewDidAppear is called every time you navigate back to the view but viewDidLoad is only called once when the view is initialized.

I would agree with earlier suggestions of hiding the scroll from the user instead of changing the way a UITableView is loading data. My suggestion would be to use the scrollToRowAtIndexPath method in the viewWillAppear method with animation set to NO. After that if you have to add a new row while the table is visible to the user, use insertRowsAtIndexPaths:withRowAnimation: to add a row at the bottom of the table view. Be sure to take care of adding the data at the end of your data model so that when the user navigates away and comes back, s/he comes back to the same layout.

Hope this helps.

edit:
Just saw your reason for not accepting the previous answers and thought I'd elaborate a little more. The solution I propose would require minimum effort, avoid calling reloadData time and again and thus avoid calling the scrollToRowAtIndexPath method again and again. You only need to make one call to scrollToRowAtIndexPath in viewWillAppear to scroll to the bottom of the table view (hiding the transition from the user when doing so) and you wouldn't need to do that again.

swift reverse order of tableview cells

In order to achieve this you will need to sort your data source (your dictionary in this case). Dictionaries by default are not ordered data structures, so you will need to create an ordered one from it (such as an array for example). For example, if you wanted to sort by the dictionaries key, then you could do something like this in Swift:

let dataSource = ["aaaaaa" : "Some data here", "bbbbbb" : "Some data here", "cccccc" : "Some data here"]
let sortedData = dataSource.sort{ $0.0 < $1.0 }

This will give you an array of tuples, which may not be the most use to you. If you wanted to order by key and then just store the data associated with it, you could append the map function like so:

let dataSource = ["aaaaaa" : "Some data here", "bbbbbb" : "Some data here", "cccccc" : "Some data here"]
let sortedData = dataSource.sort{ $0.0 < $1.0 }.map { $0.1 }

After sorting, remember you refresh your table view. You can do this by either refreshing the cells, the sections or the entire thing. Easiest way is like this:

tableView.reloadData()

Hope this helps you out :)

Tableview won't fully scroll to the bottom

the is because of tabBar it is hide the table view so you can add this code to your ViewController

let adjustForTabbarInsets: UIEdgeInsets = UIEdgeInsets(0, 0, self.tabBarController!.tabBar.frame.height, 0)
self.yourTableView.contentInset = adjustForTabbarInsets
self.yourTableView.scrollIndicatorInsets = adjustForTabbarInsets

Showing the last cell of Tableview

try use this

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [tableView] in
if myArray.count > 0 {
tableView.scrollToRow(at: IndexPath(item:myArray.count - 1 , section: 0), at: .bottom, animated: false)
}
}

Add New data from Bottom in TableView

I am able to bind Data from bottom of TableView. All credit goes to Rajeshkumar R for helping me.

Step 1, apply a transform to the table view rotating it 180deg

tableView.transform = CGAffineTransformMakeRotation(-M_PI);

Step 2, rotate your raw cell 180deg in tableView:cellForRowAtIndexPath:

cell.transform = CGAffineTransformMakeRotation(M_PI);

Step 3, reverse your datasource. If you're using an NSMutableArray insert new objects at location 0 instead of using AddObject...
Now, the hard part is remembering that left is right and right is left only at the table level, so if you use

[tableView insertRowsAtIndexPaths:targetPath withRowAnimation:UITableViewRowAnimationLeft]


Related Topics



Leave a reply



Submit