[英]Swift 3 Table View - Remove Stack View from some cells
I've got table view cells which contain, amongst other things, a stack view. 我有一个表格视图单元格,其中除其他外还包含一个堆栈视图。 The stack view should only be in a cell, if some requirements are true. 如果满足某些要求,则堆栈视图应仅在单元格中。 If not, then the height of the cell should be reduced. 如果不是,则应降低电池的高度。 When I use .isHidden, the height stays the same. 当我使用.isHidden时,高度保持不变。 But I want the stack view to be removed from that cell. 但是我希望从该单元格中删除堆栈视图。
Here's my code: 这是我的代码:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RumCell", for: indexPath) as! RumCell
let currentRum: Rum
currentRum = rumList[indexPath.row]
cell.rum = currentRum
if (cell.rum?.clubRatingJuicy == 0) && (cell.rum?.clubRatingGasy == 0) && (cell.rum?.clubRatingSpicy == 0) && (cell.rum?.clubRatingSweet == 0) {
cell.frame.size.height -= 76
}
return cell
}
As you can see, I tried to reduce the cell height, but this doesn't work. 如您所见,我试图降低像元高度,但这是行不通的。 I also tried this, which doesn't work: 我也试过了,这行不通:
if (cell.rum?.clubRatingJuicy == 0) && (cell.rum?.clubRatingGasy == 0) && (cell.rum?.clubRatingSpicy == 0) && (cell.rum?.clubRatingSweet == 0) {
cell.tastStack.removeFromSuperview()
}
Can anyone please tell me how to do this? 谁能告诉我该怎么做?
Try code for dynamic height, and Give constraint without fix height in tableView cell 尝试代码以获取动态高度,并在tableView单元格中给出没有固定高度的约束
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return 100.0
}
You should not be setting cell frames. 您不应该设置单元格框架。 That's not how TableViews work. 这不是TableViews的工作方式。 If the cell heights are dynamic then @Theorist is correct. 如果像元高度是动态的,则@Theorist是正确的。 If not, you can implement 如果没有,您可以实施
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
if let cell = tableView.cellForRowAtIndexPath(indexPath), let rum = cell.rum, rum.clubRatingJuicy == 0 && rum.clubRatingGasy == 0 && rum.clubRatingSpicy == 0 && rum.clubRatingSweet == 0 {
return {no stackview height} //whatever the height should be for no stackview
}
return {normal height} //whatever your value is
}
you should use different cell prototypes RumCell
(without stackview) and RumCellDetailed
(with stackview) which both conform to a protocol RumCellProtocol
(where you can set the rum
var) 您应该使用不同的单元原型RumCell
(不带stackview)和RumCellDetailed
(带stackview),它们都符合协议RumCellProtocol
(可以在其中设置rum
var)
protocol RumCellProtocol {
func config(rum: Rum)
}
and this code: 和此代码:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cellIdentifier = "RumCellDetailed"
if (cell.rum?.clubRatingJuicy == 0) && (cell.rum?.clubRatingGasy == 0) && (cell.rum?.clubRatingSpicy == 0) && (cell.rum?.clubRatingSweet == 0) {
cellIdentifier = "RumCell"
}
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RumCellProtocol
let currentRum: Rum
currentRum = rumList[indexPath.row]
cell.config(rum: currentRum)
return cell
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.