2017-05-04 48 views
1

複雜視圖我需要顯示一些的UIImageView,文本和鏈接查看我的TableViewController如果名單控制器是空的。斯威夫特 - 顯示的UITableViewController與空單,通過故事板

我知道你可以通過這個代碼中插入一個標籤:

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.emptyView 

凡emptyView以這種方式被定義

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

    ... 
} 

但它不工作,我看不到在表視圖如果列表是空的。

我該怎麼辦?

謝謝你很多

回答

0

最好的辦法是用兩個容器查看一個用的tableview和一個與emptyVC,然後在容器可以隱藏/取消隱藏或刪除/取決於你的邏輯添加視圖的父

+0

肯定的內容,但我想有一個不同的控制器,其中插入UI組件 – dayroma

+0

集裝箱控制器,你可以有嵌入父控制器不同的控制器 –

0

EmptyViewController都有自己的view,你爲什麼要創建另一個emptyView

遠程emptyView屬性,並使用此代碼:

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let emptyViewController = storyboard.instantiateViewController(withIdentifier :"empty_controller") as! EmptyViewController 
tableView.backgroundView = emptyViewController.view 
0

設置您查看控制器從的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部分。您可以編輯一下這個標題是基於表