Swift - 在 UITableViewController 中通过 Storyboard 显示一个带有空列表的复杂 View

标签 swift uitableview uiviewcontroller empty-list

如果列表 Controller 为空,我需要在我的 TableViewController 中显示一个包含一些 UIImageView、文本和链接的 View 。

我知道你可以通过这段代码插入一个标签:

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
}

但我需要在不同的 ViewController 中显示 Storyboard 中设计的复杂 View 。

我试过这个:

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?

  ...
}

但这不起作用,如果列表为空,我在表中看不到该 View 。

我该怎么做?

非常感谢

最佳答案

最好的办法是使用两个容器 View ,一个带有 tableview,另一个带有 emptyVC,然后在该容器的父级中,您可以根据您的逻辑隐藏/取消隐藏或删除/添加 View

关于Swift - 在 UITableViewController 中通过 Storyboard 显示一个带有空列表的复杂 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43776681/

相关文章:

ios - UITableView _endCellAnimationsWithContext 断言失败

ios - UITableView 背景颜色在 iPad 上总是白色

iphone - 无法更改自定义 UITableViewCell 中的透明 BG(分组 TableView )

iphone - 为什么我不能在我的 iPhone 静态库中的包中加载 nib?

ios - 使用 View Controller 的更好方法是什么?

ios - Table View加载时接收数据

ios - SWIFT_VERSION 每次 pod 更新时都会出错

初始化期间的 Swift 可选值防止默认初始化程序继承

ios - 更改 UIPageViewController 中显示的初始 View Controller

ios - 单元测试 iOS 应用程序扩展