Display scrolling table down when using dynamic cell height

How can I scroll the table view down to use dynamic cell height?

For some reason, this code does not work in this scenario:

[self.tableView scrollRectToVisible:CGRectMake(0, self.tableView.contentSize.height - self.tableView.bounds.size.height, self.tableView.bounds.size.width, self.tableView.bounds.size.height) animated:YES]; 

Thanks!

EDIT . Use the @Hasiya code to scroll to the bottom, for some of you this may do the trick.

+7
ios objective-c xcode uitableview
source share
5 answers

In the end, I found the answer . The reason that scrolling to the bottom doesn't work (and inserting / deleting rows is also wrong, as I found out later), is because the cell height is not properly estimated . To get around this, try returning close estimates to estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath . However, if you cannot do this (and in most cases you cannot), there is still a great solution: Store the cell heights in the dictionary and return them for estimatedHeight when the cells (for example, the table view scrolls). I found this solution in another question, so please go and check it for the actual code on how you performed it. (although probably many of you can write this for yourself)

Original solution from @dosdos

EDIT: Perhaps fixing just scrolling to the bottom is not necessary, but highly recommended, in my opinion. If you do not use this to estimate row height, you will run into other problems, such as error scrolling and poor performance in 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) 
+3
source share

The following worked for my Swift 3.1 code using Xcode 8.3.2 focused on iOS 10.1 for iPhone only.

I had the same problem as the OP. My app included a messaging feature. I wanted the last message to be visible when the message view was pushed onto the stack. High table cell heights change due to message content. None of the solutions satisfied me (the last row of the table was not visible or only partially visible). But here is what worked for me:

  • In Interface Builder (IB), set the row height of the cell to a value greater than I expected that my average row height will be 50 in my case (be sure to check the "Custom" box).
  • In the ViewController, where the tableview is implemented inside the viewDidLoad function, set the tableview 'rowHeight' property as follows:

     myTableView.rowHeight = UITableViewAutomaticDimension 
  • Also, the function 'viewDidLoad' is set to tableRownHeight tableview for a value higher than you expect the average row height to be. In my case, 140.

Finally, and most importantly, I wrote this bit of code:

 extension UITableView { func scrollToBottom() { let rows = self.numberOfRows(inSection: 0) let indexPath = IndexPath(row: rows - 1, section: 0) self.scrollToRow(at: indexPath, at: .top, animated: true) } } 

Pay attention to at: .top is what solved my problem. Passing the .bottom value for the UITableViewScrollPosition parameter just didn't work. Even passing .middle worked - but not .bottom .

So, whenever my dataset needs to be updated or a new message arrives, I call

 func getTableViewData(){ // Get your data here any way you usually do.. // call to reload myTableView.reloadData() // call the scroll function myTableView.scrollToBottom() } 

I checked the results of this “method” by simply changing the at: .bottom to at: .top . Each time I changed it to at: .top , the last rows of the table were completely visible, and the table scroll to the end.

+7
source share

Hope this might work for you. try it

 [self.tableview setContentOffset:CGPointMake(0, CGFLOAT_MAX)] 

or

 if (self.tableview.contentSize.height > self.tableview.frame.size.height) { CGPoint offset = CGPointMake(0, self.tableview.contentSize.height - self.tableview.frame.size.height); [self.tableview setContentOffset:offset animated:YES]; } 
+1
source share

Try using scrollToRowAtIndexPath as indicated in the code below.

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; NSInteger no = array.count-1; NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:no inSection:0]; [self.tableView scrollToRowAtIndexPath:lastIndex atScrollPosition:UITableViewScrollPositionBottom animated:YES]; } 
0
source share

I earned by following these simple steps:

1) in IB, set the height of the estimate manually for a higher expected value 2) in viewWillAppear scroll to the last line with the ScrollPosition parameter as UITableViewScrollPositionTop

0
source share

All Articles