IOS - UITableView doesn't scroll to bottom

I am creating a chat application in iOS, but I have some problems displaying a UITableView of all messages. I want the table view to be scrolled to the bottom of the download so that users can see the latest messages when they open the application.

To do this, I added this code to my function, updating the data:

NSIndexPath* ipath = [NSIndexPath indexPathForRow: [self.messageOnTimeline numberOfRowsInSection:0]-1 inSection: 0]; [self.messageOnTimeline scrollToRowAtIndexPath: ipath atScrollPosition: UITableViewScrollPositionTop animated: YES]; 

This worked fine until I added a title to the table. I added the following code, and now when I download the application, it does not scroll down.

 -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)]; return view; } 

Does anyone know why?

Many thanks for your help

+8
ios header uitableview scroll scrollto
source share
4 answers

Add this code to yourself viewDidload :

Goal c

  NSIndexPath *myIndexPath = [NSIndexPath indexPathForRow:AddYourLastIndex inSection:0]; [self.tbl_presentation selectRowAtIndexPath:myIndexPath animated:YES scrollPosition:UITableViewScrollPositionBottom]; 

Swift

 var myIndexPath: NSIndexPath = NSIndexPath.indexPathForRow(AddYourLastIndex, inSection: 0) self.tbl_presentation.selectRowAtIndexPath(myIndexPath, animated: true, scrollPosition: UITableViewScrollPositionBottom) 

OR Check this link

I hope this code is helpful to you.

+7
source share

try this .it code will scroll to the bottom of the table.

  [self.tableview setContentOffset:CGPointMake(0, self.tableview.contentSize.height-self.tableview.frame.size.height) animated:YES]; 
+3
source share

@ Darshan Kunyadiya answers correctly. Even we can achieve the same in the storyboard.

Select tableView β†’ from the frame control, click Add Missing Constraints. This will add a restriction on tableView and View.

It helped me solve this problem. Screen screen_shot is included for reference. screen_shot Link

0
source share

Swift 5, iOS 12 tested

Code This code works fine for a UITableView , even if there are sections with 0 elements (which will crash in any other solution I have seen on the Internet)

 extension UITableView { func scrollTableViewToBottom(animated: Bool) { guard let dataSource = dataSource else { return } var lastSectionWithAtLeasOneElements = (dataSource.numberOfSections?(in: self) ?? 1) - 1 while dataSource.tableView(self, numberOfRowsInSection: lastSectionWithAtLeasOneElements) < 1 { lastSectionWithAtLeasOneElements -= 1 } let lastRow = dataSource.tableView(self, numberOfRowsInSection: lastSectionWithAtLeasOneElements) - 1 guard lastSectionWithAtLeasOneElements > -1 && lastRow > -1 else { return } let bottomIndex = IndexPath(item: lastRow, section: lastSectionWithAtLeasOneElements) scrollToRow(at: bottomIndex, at: .bottom, animated: animated) } } 

Code This code works for UIScrollView , but will not work for UITableView , which has more cells than one screen can accommodate, due to the strange internal implementation of Apple's UITableViewController :

 extension UIScrollView { func scrollToBottom(animated: Bool) { guard contentSize.height > bounds.size.height else { return } let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom) setContentOffset(bottomOffset, animated: true) } } 
0
source share

All Articles