2016-08-22 117 views
2

我有一個TableViewController,它包含三個不同的自定義單元格。對於第一個單元格,我想包含一個水平滾動的CollectionView,第二個單元格包含一個CollectionView,第三個單元格包含一個垂直的CollectionView。我寫我的代碼是這樣的:Swift:如何在包含多個單元格的TableViewController中設置動態單元格高度

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 3 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 


    if indexPath.section == 0 { 

     let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell1") as! HomePageTableViewCell1 


     return cell 
    } 

    else if indexPath.section == 1 { 

     let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell2") as! HomePageTableViewCell2 


     return cell 
    } 
    else { 

     let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell3") as! HomePageTableViewCell3 


     return cell 
    } 

} 

它的工作原理就像我想的,但我不確定這是實現我想要的最好方法嗎?因爲我對Swift很陌生,也許有更好的方法來做到這一點?其次,對於這三個單元格,我想保留一個基於內容的動態高度。例如,第一個單元格包含一個ImageView,其高度爲260,第二個單元格包含一個高度爲140的ImageView。第三個單元格中包含的CollectionView將返回4行,每行中,有一個與高度96.我已經做了ImageView的是:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if indexPath.section == 0 { 
     return 260 
    } 
    else if indexPath.section == 1 { 
     return 140 
    } 
    else { 
     return 500 

    } 
} 

但這意味着我必須硬編碼這些高度在我知道內容的高度。但是如果我不知道身高,比如說第三個細胞會返回5排,這意味着500不夠,那麼我應該怎麼做才能給細胞提供一個動態的高度。

+0

使用heightforRowAtIndexPath設置每個單元的高度 –

+0

如何識別各小區?使用'indexPath.section'? –

+0

是使用indexpath.section == 0像這樣,管理每個單元格的高度的最佳方法 –

回答

1

這是可能的:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    if indexPath.section == 0 { 

     let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell1") as! HomePageTableViewCell1 

     let string1 = "blablabla" 
     let charCountString1 = string1.character.characters.count 

     tableView.rowHeight = CGFloat(charCountString1) 
     return cell 
    } 

    else if indexPath.section == 1 { 

     let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell2") as! HomePageTableViewCell2 

     let imageHeight = imageView.frame.height 

     tableView.rowHeight = CGFloat(imageHeight) 
     return cell 
    } 
    else { 

     let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell3") as! HomePageTableViewCell3 

     let string3 = "ldsakjfalksdjflk" 
     let charCountString3 = string3.character.characters.count 

     tableView.rowHeight = CGFloat(charCountString3) 
     return cell 
    } 

} 
+0

,以便我可以在'cellForRowAtIndexPath'中設置高度,這很好。但'50,100,150'是根據需要設定的?所以對我來說,它會是'260,140,​​500'吧?但我想要動態細胞高度,我該怎麼做?謝謝 –

+0

是的,你可以感受到50,100,150與你想要的任何值。這些值可以動態構建,我將調整示例來演示。明白了嗎? –

+0

我明白了。但是當單元格返回'ImageView'而不是'string'時怎麼樣?謝謝 –

0

你總是可以爲您的tableView內容的基礎上動態的高度,沒有必要在你的細胞返回靜態高度剛剛設置的約束正確始終對content-hugging content-compressionResistance使用首選優先級。

在-viewDidLoad 剛剛成立

self.tableView.rowHeight = UITableViewAutomaticDimension 
self.tableView.estimatedRowHeight = 60; // or whatever is suitable for you 

你也可以參考從雷Wenderlich的教程 Self-sizing Table View Cells

+0

謝謝,我會看看 –

相關問題