Swift:如何在包含多个单元格的 TableViewController 中设置动态单元格高度 [英] Swift: How to set dynamic cell height in TableViewController which contains more than one cell
问题描述
我有一个 TableViewController,它包含三个不同的自定义单元格.对于第一个单元格,我想包含一个水平滚动的 CollectionView,第二个单元格也包含一个 CollectionView,第三个单元格包含一个垂直的 CollectionView.我这样写我的代码:
I have a TableViewController, which contains three different customized cell. For the first cell, I want to contain a horizontal scrolled CollectionView, the second cell contains a CollectionView as well, and the third one contains a vertical CollectionView. I write my code like this:
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 的新手,也许有更好的方法来做到这一点?
it works like I want, but I am not sure is this the best way to achieve what I want? cause I am new to Swift, maybe there are some better way to do this?
其次,对于这三个单元格,我想保留基于内容的动态高度.例如,第一个单元格包含一个高度为 260 的 ImageView,第二个单元格包含一个高度为 140 的 ImageView.第三个单元格包含一个 CollectionView,它将返回 4 行,在每一行中,有一个高度为 96 的 ImageView.我所做的是:
Secondly, for these three cell, I want to retain dynamic height based one the content. for example, the first cell contains a ImageView which height is 260, the second cell contains a ImageView with 140 of the height. the third cell contains a CollectionView which will return 4 rows, and in each row, there is a ImageView with height 96. what I have done is :
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还不够,那我该怎么做才能给单元格一个动态高度.
but this means I have to hard code these height in the case I know the height of the content. But what if I don't know the height, like if the third cell will return 5 row, which means 500 is not enough, then what should I do to give the cell a dynamic height.
推荐答案
这是可能的:
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
}
}
这篇关于Swift:如何在包含多个单元格的 TableViewController 中设置动态单元格高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!