2016-08-22 22 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

谢谢,我会看看 –

相关问题