2017-05-04 5 views
1

リストコントローラが空の場合、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 
} 

しかし、私は別の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? 

    ... 
} 

しかし、それは動作しないリストならば、私はテーブルにそのビューを見ることができません空です。

どうすればいいですか?

答えて

0

最善の策は、そのコンテナの親にあなたのロジック

に応じて、ビューを/非表示再表示または削除/追加することができ、テーブルビューで1を表示し、emptyVCと1二つの容器を使用することですあなたにたくさんありがとうございました
+0

はい、私は別のコントローラを持っていたいと思います。UIコンポーネントを挿入します – dayroma

+0

コンテナコントローラを使用すると、 –

0

EmptyViewControllerには独自のviewがありますが、なぜ別のemptyViewを作成する必要がありますか?

リモートemptyViewプロパティと、このコードを使用:あなたはUITableViewDelegateを継承し、あなたのVCを指すようにtableView.delegateを設定するためのコントローラを表示

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

セット。その後

class VC : UITableViewController, UITableViewDelegate { 
    override func viewDidLoad() { 
     ... 
     tableView.delegate = self 
    } 
} 

これは、あなたのテーブルビューのセクションにヘッダを追加します次の2つの関数

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

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

} 

をオーバーライドします。このヘッダがテーブルの内容に基づいて編集することができます

関連する問題