How to Scroll to the Exact End of the Uitableview

Scroll all the way down to the bottom of UITableView

If your requirement is to scroll to the last cell of tableView you can use setContentOffset like this so that you can scroll to the last cell of tableView.

Put this in the reloadData() function:

func reloadData(){
chatroomTableView.reloadData()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let scrollPoint = CGPoint(x: 0, y: self.chatroomTableView.contentSize.height - self.chatroomTableView.frame.size.height)
self.chatroomTableView.setContentOffset(scrollPoint, animated: true)
})
}

Add this to your UITableViewDelegate :

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let lastRowIndex = tableView.numberOfRowsInSection(0)
if indexPath.row == lastRowIndex - 1 {
tableView.scrollToBottom(true)
}
}

Add this to the bottom of your .swift file:

extension UITableView {
func scrollToBottom(animated: Bool = true) {
let sections = self.numberOfSections
let rows = self.numberOfRowsInSection(sections - 1)
if (rows > 0){
self.scrollToRowAtIndexPath(NSIndexPath(forRow: rows - 1, inSection: sections - 1), atScrollPosition: .Bottom, animated: true)
}
}
}

Auto scroll to bottom of UITableView

let indexPath = IndexPath(item: noOfRows, section: 0)
yourTableView.scrollToRow(at: indexPath, at: UITableView.ScrollPosition.bottom, animated: true)

UITableView scroll to bottom

Scrolling to tableViewCell?

//for instance, you have 15 cells
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:14 inSection:0];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop
animated:YES];

Scroll table view to bottom when using dynamic cell height

Eventually, I have found the answer. The reason why scrolling to the bottom does not work (and inserting/deleting rows are buggy as well, as I found out later), is because cell height is not properly estimated. To get around this, try to return close estimations in estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath. However, if you can't do that (and in most cases you won't be able to) there is still a great solution: Store the heights of the cells in a dictionary, and return those for the estimatedHeight when cells are reused (e.g. table view is scrolled). I have found this solution in another question, so please go and check that out for the actual code on how you would carry this out. (although probably, many of you can write this for themselves)

Original solution by @dosdos

EDIT: Maybe to fix just scrolling to the bottom this is unnecessary, however highly recommended in my opinion. If you don't use this for estimating your row height, you will encounter other problems such as buggy scrolling and worse performance at a large number of cells.

Also you need to add this to viewDidAppear

let lastItem = IndexPath(item: dataSource.count - 1, section: 0)
tableView.scrollToRow(at: lastItem, at: .bottom, animated: true)

How to remember exact scroll position in table view

QUESTION: I just saw that you mentioned that contentOffset wasn't working for you. Can you try the method again by printing out the contentOffset value each time? That's a good starting point to understand why the offset method is not working for you.

Use the following to save the scroll position:

 let offset = tableView.contentOffset

Then set the scroll position as follows:

tableView.setContentOffset(offset, animated: false)

How to scroll to the bottom of a UITableView on the iPhone before the view appears

I believe that calling

 tableView.setContentOffset(CGPoint(x: 0, y: CGFloat.greatestFiniteMagnitude), animated: false)

will do what you want.

How to know when UITableView did scroll to bottom in iPhone?

The best way is to test a point at the bottom of the screen and use this method call when ever the user scrolls (scrollViewDidScroll):

- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point

Test a point near the bottom of the screen, and then using the indexPath it returns check if that indexPath is the last row then if it is, add rows.



Related Topics



Leave a reply



Submit