简体   繁体   English

UITableView 未显示在 UIScrollView 内

[英]UITableView is not showing inside a UIScrollView

I have in my View a long UIScrollView (about 1000 px in height), and at the end of this UIScrollView I have a UITableView.我的视图中有一个很长的 UIScrollView(高度约 1000 像素),在这个 UIScrollView 的末尾我有一个 UITableView。

The cellForRowAtIndexPath is never called (surely i checked the delegate and datasource if they are connected right, and the IBOutlet of the table is strong) but numberOfRowsInSection is getting called. cellForRowAtIndexPath 从未被调用(我当然检查了委托和数据源是否连接正确,并且表的 IBOutlet 很强)但 numberOfRowsInSection 被调用。

I tried reloading the TableView when the UIScrollView scrolls so when the table is at focus the cellForRowAtIndexPath might get called, but with no luck.我尝试在 UIScrollView 滚动时重新加载 TableView,因此当表格处于焦点时,可能会调用 cellForRowAtIndexPath,但没有运气。

Did anyone encounter a similar behaviour when trying to use tableview and scrollview together?有没有人在尝试同时使用 tableview 和 scrollview 时遇到类似的行为?

Your hierarchy is like this:你的层次结构是这样的:

A parentView is there.有一个 parentView。 Inside the parent view there is a scroll view and there is a table view.在父视图中有一个滚动视图和一个表视图。 So, your tableview is somewhere at 1000 from origin of parentview.因此,您的 tableview 距离 parentview 的原点在 1000 处。

So, tableview will never become visible to your parentview and no delegates will be fired.因此,tableview 永远不会对您的父视图可见,并且不会触发任何委托。

Include your view as a headerView of your UITableView like that:将您的视图作为 UITableView 的 headerView 包含在内:

在此处输入图片说明

Update for 2020, swift 5.X. 2020 年更新,swift 5.X。 To create a custom UITableView that allows your tableview inside a scrollview !创建一个自定义的 UITableView,允许你的tableviewscrollview

1) Create a subclass of UITableView: 1)创建一个UITableView的子类:

import UIKit导入 UIKit

final class ContentSizedTableView: UITableView { override var contentSize:CGSize { didSet { invalidateIntrinsicContentSize() } }最终类 ContentSizedTableView: UITableView { 覆盖 var contentSize:CGSize { didSet { invalidateIntrinsicContentSize() } }

override var intrinsicContentSize: CGSize {
    layoutIfNeeded()
    return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
}

} 2) Add a UITableView to your layout and set constraints on all sides. 2) 将 UITableView 添加到您的布局并在所有方面设置约束。 Set the class of it to ContentSizedTableView.将它的类设置为 ContentSizedTableView。

3) You should see some errors, because Storyboard doesn't take our subclass' intrinsicContentSize into account. 3) 您应该会看到一些错误,因为 Storyboard 没有考虑我们的子类的intrinsicContentSize At runtime it will use the override in our ContentSizedTableView class在运行时,它将使用我们的 ContentSizedTableView 类中的覆盖

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

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