简体   繁体   English

UITableView 不会滚动到底部(Swift IOS)

[英]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).我的 UITableView 不会滚动到底部并在显示所有内容之前停止(切断最后几行)。 I have checked that my UITableView is equal to the screen size, but the content size is greater than screen and table height.我已经检查过我的 UITableView 是否等于屏幕大小,但内容大小大于屏幕和表格高度。 I am not sure why this is happening.我不确定为什么会这样。

1) Do you have a UITabBar at the bottom of your screen? 1)你的屏幕底部有UITabBar吗? 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.如果是这样,请转到您的故事板和您的UIViewController您将UITableView放入并取消选中它是Under Bottom Bars布尔值。

在此处输入图片说明

2) You might have an NSLayoutConstraint set at the bottom that is to the negative. 2)您可能在底部设置了一个NSLayoutConstraint为负值。 Meaning your UITableView will be extending to the bottom of the screen.这意味着您的UITableView将延伸到屏幕底部。 IF so, reset your UITableView 's bottom constraint to 0.0.如果是这样,请将UITableView的底部约束重置为 0.0。

在此处输入图片说明

3) You might want to try using UITableView scroll method. 3)您可能想尝试使用UITableView滚动方法。 Get the index of the last cell:获取最后一个单元格的索引:

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

4) UITableView is subclassed from UIScrollView . 4) UITableViewUIScrollView子类。 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:如果UITableView中的单元格具有相同的高度,您可以将数据源中的对象数量乘以单元格的高度,然后您就知道要在UIScrollView/TableView方法中输入什么值作为 contentOffset:

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

That most probably happen in small iPhone devices.这很可能发生在小型 iPhone 设备中。 because you didnt set your bottom constraint of the tableview correctly.因为您没有正确设置 tableview 的底部约束。 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 固定到超级视图,如果您使用自定义 tableview 单元格,然后将所有标签约束(前导、尾随、顶部和底部)固定到单元格,然后在您的 ViewController viewDidLoad方法中编写以下代码:-

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如果您使用UITableViewAutomaticDimension不要使用 tableview 委托方法(heightForRowAtIndexPath)来调整高度

In my case...removing在我的情况下......删除

tableview.frame.size = tableview.contentsize worked tableview.frame.size = tableview.contentsize 有效

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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