简体   繁体   中英

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.

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.

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.

Did anyone encounter a similar behaviour when trying to use tableview and scrollview together?

Your hierarchy is like this:

A parentView is there. 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.

So, tableview will never become visible to your parentview and no delegates will be fired.

Include your view as a headerView of your UITableView like that:

在此处输入图片说明

Update for 2020, swift 5.X. To create a custom UITableView that allows your tableview inside a scrollview !

1) Create a subclass of UITableView:

import UIKit

final class ContentSizedTableView: UITableView { override 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. Set the class of it to ContentSizedTableView.

3) You should see some errors, because Storyboard doesn't take our subclass' intrinsicContentSize into account. At runtime it will use the override in our ContentSizedTableView class

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