简体   繁体   中英

UITableView does not scroll to bottom (Swift IOS)

My UITableView does not scroll to the bottom and stops before all the content can be displayed (cuts off the last few rows). I have checked that my UITableView is equal to the screen size, but the content size is greater than screen and table height. I am not sure why this is happening.

1) Do you have a UITabBar at the bottom of your screen? This content may be covered by the bar. IF so, go your storyboard and to your UIViewController you have the UITableView placed in and uncheck it's Under Bottom Bars boolean.

在此处输入图片说明

2) You might have an NSLayoutConstraint set at the bottom that is to the negative. Meaning your UITableView will be extending to the bottom of the screen. IF so, reset your UITableView 's bottom constraint to 0.0.

在此处输入图片说明

3) You might want to try using UITableView scroll method. Get the index of the last cell:

[yourTableView scrollToRowAtIndexPath:lastCellIndex 
                 atScrollPosition:UITableViewScrollPositionTop 
                         animated:YES];

4) UITableView is subclassed from UIScrollView . Therefore you can treat it as such.

If ever cell in the UITableView has the same height, you can multiple the number of objects in your datasource times the height of the cell, then you know what value to enter into your UIScrollView/TableView method as contentOffset:

(void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated

That most probably happen in small iPhone devices. because you didnt set your bottom constraint of the tableview correctly. Check that constraint. if it doesnt exist add it.

May be you need to set the automatic dimensions:-

You can pinned your tableView to the superview with the correct constraint and if you are using the custom tableview cell then pinned all the labels constraint (leading, trailing, top and bottom)to the cell and then inside your ViewController viewDidLoad method write the below code:-

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44.0

Once your constraints are correct then your table view cell content size will resize dynamically depends on the content.

Note:-

Do not use the tableview delegate method (heightForRowAtIndexPath) to adjust the height if you are using UITableViewAutomaticDimension

In my case...removing

tableview.frame.size = tableview.contentsize worked

从属性检查器内的故事板取消选中各个 ViewController 的“Under Bottom Bars”解决了我的问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM