2016-12-02 132 views
0

我有一個表格視圖,cells使用自動佈局constraints動態增長。我有一個場景,我必須在這些動態構建的單元之一中顯示UITableView。這裏最主要的是我不知道任何東西的框架,我必須使用約束來構造一切。我需要的是解釋如何實現這一目標?具有動態高度和自動佈局約束的UITableView

+0

請通過以下方式:https://iosstuff.wordpress.com/2011/06/29/adding-a-uitableview-inside-a-uitableviewcell/ –

+0

訪問這些鏈接可能會幫助你http://stackoverflow.com/questions/26752645/dynamic-uitablecellview-height – Aravi

+3

可能的dyblicate http://stackoverflow.com/q/18527227/2012219 – gbk

回答

0

所以我想你有一個MasterViewController,它有一個tableView。然後,您想添加另一個屬於類MyCustomcell的一部分的tableView,它位於MasterViewController的tableView的一個單元格內。

使用tableView添加customCell並不是什麼大問題,但根據要在CustomCell中顯示的數據計算和維護MasterViewController的高度有點棘手。如果你只是問如何在tableView中添加CustomCell,那麼有許多很好的教程可以通過簡單的谷歌輕鬆獲得。

我面臨類似的情況,並以下面的方式解決了問題。希望這可以幫助。

1)如果你有一個customCell,你只需要顯示文本。然後MasterViewController的tableView的高度可以根據文本的內容自動地自動維護並自動佈局。 This tutorial有很好的解釋。

2)如果你在一個MasterViewcontroller tableView的單元格中添加tableView,那麼它變得棘手。現在MasterViewController的單元高度取決於CustomCell tableview高度的高度。我試圖通過在Swift 3中應用不同的方式來處理這種情況,但最終通過以下方式得到了結果: -

class MasterViewController: UIViewController { 
      @IBOutlet weak var tableView: UITableView! 

      var customCell:MyCustomCell? = nil 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "myCustomCell", for: indexPath) as! MyCustomCell 
     cell.selectionStyle = UITableViewCellSelectionStyle.none 
     customCell = cell 
     return cell 
    } 

} 

extension MasterViewController:UITableViewDelegate{ 

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 

     if let aHeight = customCell?.tableView.contentSize.height{ 
      return aHeight 
     } 

     return 0 

    } 

} 
+0

我必須適用於UITableView(它是在UITableViewCell中)的約束是什麼? –

+0

你可以只添加鉛,尾跡,頂部,底部到0. – skJosh

+0

我做到了,但tableview的高度現在爲零。 –