简体   繁体   中英

How to display a UITableView, which is placed inside a UIView .xib file?

Loading a custom UIView into the ViewController is simple:

if let randomView = Bundle.main.loadNibNamed("RandomView", owner: self, options: nil)?.first as? RandomView {

        self.view.addSubview(randomView)

    }

This randomView then usually also displays any labels, text fields, etc. which are placed inside of its .xib file. However, if I place a UITableView in its .xib file, it does not get displayed at all.

To add to the confusion: If I create a separate xib file and only place a UITableView inside of it, I can load it using Bundle.main.loadNibNamed and it gets displayed correctly.

This it not about setting up delegates or data sources I think. The bare empty table itself doesn't get displayed when it's placed inside of randomView's .xib file. Although the other elements like labels, text inputs, etc. do.

The UITableView in the separate .xib file gets loaded and displayed correctly without any delegates or datasources set up.

The first example doesn't display the table, the second one does. 描述 内容描述2

该问题是由于缺少约束(请参见第一张图片中的红色箭头)引起的,从而导致无法正确显示表格视图。

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