2015-12-14 33 views
3

我有一個UITableView,使用UITableViewAutomaticDimension和AutoLayout約束自身大小的單元高度。在原型細胞之一,我需要添加UICollectionViewUICollectionViewFlowLayout將垂直佈局單元格在緊湊型類或3列常規尺寸類的網格一列。集合視圖的高度因此是動態的,即使在iPhone 6 Plus(Compact-> Regular)上旋轉,佈局也可能會改變。可變高度CollectionView在iOS8中使用AutoLayout自動調整大小的TableView單元內部+

我在故事板中設置了自定義原型單元格的tableview,並將自定義collectionview添加到原型單元格中,AutoLayout約束始終爲零到超視圖的對應邊緣(前導,尾隨,頂部,底部= 0到超級視圖)。 .. collectionView-> tableviewContentView-> tableviewCell。

我所需要的集合視圖的可變高度來驅動的tableview原型細胞的高度。到目前爲止,我總是在運行時遇到以下AutoLayout錯誤。

Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead. 

你能否建議正確的方法呢?謝謝!

主要TableViewController:

class TableViewController: UITableViewController { 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView.estimatedRowHeight = 200.0 
     tableView.rowHeight = UITableViewAutomaticDimension 
    } 
    /// ... plus tableview datasource functions 
} 

的CollectionView:

class AttachmentCollectionView: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegate { 

    override func awakeFromNib() { 
     let nib = UINib(nibName: Constants.AttachmentCollectionView.cellNibName, bundle: nil) 
     registerNib(nib, forCellWithReuseIdentifier: Constants.AttachmentCollectionView.cellIdentifier) 
     collectionViewLayout = attachmentFlowLayout() 
     dataSource = self 
     delegate = self 
     backgroundColor = UIColor.whiteColor() 
    } 

    private func attachmentFlowLayout() -> UICollectionViewFlowLayout { 
     let layout = UICollectionViewFlowLayout() 
     layout.scrollDirection = .Vertical 
     layout.itemSize = CGSize(width: 70, height: 50) 
     layout.sectionInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0) 
     layout.minimumLineSpacing = 0.0 
     layout.minimumInteritemSpacing = 0.0 
     layout.headerReferenceSize = CGSizeMake(0.0, 0.0) 
     layout.footerReferenceSize = CGSizeMake(0.0, 0.0) 
     return layout 
    } 
    // ...plus collectionview datasource functions 
} 
+0

如果你用完它,那麼你可以給我演示代碼吧,我有表視圖5節,最後一節一排有一個集合視圖,沒有滾動,我想它的動態動態大小表格單元大小 – RaviJSS

回答

1

我想實現的tableView的委託方法tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat可能工作。

+1

它可能會工作,但這是iOS8之前的自我大小的tableview單元格的方法。我正在尋找一種方式,如果它甚至可能,集體視圖的內容將如何通過自動佈局自動驅動tableview單元格的高度。 –

+0

@MartinKoles:閱讀http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift後,我猜你的問題可能是您放置在約束你的CollectionView。我的猜測是CollectionView沒有高度約束,然後不允許tableViewCell確定它的內容大小。 – dudeman

+0

謝謝,但經過快速審查,我沒有看到閱讀涉及滾動視圖子類像tableview或collectionview的任何動態內容。我對集合視圖有垂直約束,但由於它是scrollview的子類,因此約束只是定位集合視圖本身,並且沒有將其內容告訴自動佈局引擎。我現在猜測,如果我簡單的使用FlowLayout甚至定義collectionview的contentSize。我不應該做一個適當的UICollectionViewLayout子類並計算contentSize嗎? –

相關問題