Swift-通过情节提要在UITableViewController中以空列表显示复杂视图

Swift-通过情节提要在UITableViewController中以空列表显示复杂视图,swift,uitableview,uiviewcontroller,empty-list,Swift,Uitableview,Uiviewcontroller,Empty List,如果列表控制器为空,我需要在TableViewController中显示一个包含一些UIImageView、文本和链接的视图 我知道您可以通过以下代码插入标签: func numberOfSections(in tableView: UITableView) -> Int { var numOfSections: Int = 0 if youHaveData { tableView.separatorStyle = .singleLine

如果列表控制器为空,我需要在TableViewController中显示一个包含一些UIImageView、文本和链接的视图

我知道您可以通过以下代码插入标签:

func numberOfSections(in tableView: UITableView) -> Int
{
    var numOfSections: Int = 0
    if youHaveData
    {
        tableView.separatorStyle = .singleLine
        numOfSections            = 1
        tableView.backgroundView = nil
    }
    else
    {
        let noDataLabel: UILabel     = UILabel(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: tableView.bounds.size.height))
        noDataLabel.text          = "No data available"
        noDataLabel.textColor     = UIColor.black
        noDataLabel.textAlignment = .center
        tableView.backgroundView  = noDataLabel
        tableView.separatorStyle  = .none
    }
    return numOfSections
}
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let emptyViewController = storyboard.instantiateViewController(withIdentifier :"empty_controller") as! EmptyViewController
tableView.backgroundView  = emptyViewController.view
但我需要在不同的ViewController中显示在故事板中设计的复杂视图

我试过这个:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let emptyViewController = storyboard.instantiateViewController(withIdentifier :"empty_controller") as! EmptyViewController
tableView.backgroundView  = emptyViewController.emptyView
其中,emptyView是以这种方式定义的

class EmptyViewController: UIViewController {
  @IBOutlet weak var emptyView: UIView?

  ...
}
但它不起作用,如果列表为空,我在表中看不到该视图

我怎么做


非常感谢

最佳选择是使用两个容器视图一个带有tableview,一个带有emptyVC,然后在该容器的父级中,您可以根据您的逻辑隐藏/取消隐藏或删除/添加视图

最佳选择是使用两个容器视图一个带有tableview,一个带有emptyVC,然后,在该容器的父级中,您可以根据您的逻辑隐藏/取消隐藏或删除/添加视图

emptyView控制器
有自己的
视图
,为什么您必须创建另一个
emptyView

远程
emptyView
属性并使用此代码:

func numberOfSections(in tableView: UITableView) -> Int
{
    var numOfSections: Int = 0
    if youHaveData
    {
        tableView.separatorStyle = .singleLine
        numOfSections            = 1
        tableView.backgroundView = nil
    }
    else
    {
        let noDataLabel: UILabel     = UILabel(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: tableView.bounds.size.height))
        noDataLabel.text          = "No data available"
        noDataLabel.textColor     = UIColor.black
        noDataLabel.textAlignment = .center
        tableView.backgroundView  = noDataLabel
        tableView.separatorStyle  = .none
    }
    return numOfSections
}
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let emptyViewController = storyboard.instantiateViewController(withIdentifier :"empty_controller") as! EmptyViewController
tableView.backgroundView  = emptyViewController.view

emptyView控制器
有自己的
视图
,为什么要创建另一个
emptyView

远程
emptyView
属性并使用此代码:

func numberOfSections(in tableView: UITableView) -> Int
{
    var numOfSections: Int = 0
    if youHaveData
    {
        tableView.separatorStyle = .singleLine
        numOfSections            = 1
        tableView.backgroundView = nil
    }
    else
    {
        let noDataLabel: UILabel     = UILabel(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: tableView.bounds.size.height))
        noDataLabel.text          = "No data available"
        noDataLabel.textColor     = UIColor.black
        noDataLabel.textAlignment = .center
        tableView.backgroundView  = noDataLabel
        tableView.separatorStyle  = .none
    }
    return numOfSections
}
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let emptyViewController = storyboard.instantiateViewController(withIdentifier :"empty_controller") as! EmptyViewController
tableView.backgroundView  = emptyViewController.view

将视图控制器设置为从UITableViewDelegate继承,并将tableView.delegate设置为指向VC

class VC : UITableViewController, UITableViewDelegate {
    override func viewDidLoad() {
        ...
        tableView.delegate = self
    }
}
然后重写以下两个函数

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

}

这将向tableview部分添加标题。您可以根据表的内容编辑此标题

将视图控制器设置为从UITableViewDelegate继承,并将tableView.delegate设置为指向VC

class VC : UITableViewController, UITableViewDelegate {
    override func viewDidLoad() {
        ...
        tableView.delegate = self
    }
}
然后重写以下两个函数

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

}

这将向tableview部分添加标题。您可以根据表的内容编辑此标题是什么

是的,但我希望有一个不同的控制器,其中插入UI组件,您可以使用容器控制器将不同的控制器嵌入到父控制器中,但我希望有一个不同的控制器,其中插入UI组件和容器控制器,您可以在父控制器中嵌入不同的控制器