UIButton inside expandable UITableViewCell doesn't dissapear

86 views Asked by At

I have UITableViewCell that becomes invisible by setting its height to zero:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return isCollapsed ? 0 : 50
}

Inside that cell I constraint every view from top to bottom of cell's view. And every view except UIButton works. But UIButton text doesn't disappear when cell's height is zero. I think it has to do with UIButton text not depending on parent height. UIButton background color does disappear when cell is collapsed. Best solution that I found is to use UILabel instead. But it doesn't have touch animation. Is there another solution without manually hiding the button?

2

There are 2 answers

1
Hitesh On

Swift 3.0

UIButton is subclass of UILabel. If your set hight to zero of button, but UILabel hight is not zero.You need to hidden that button. then its not appear in cell while hight of cell is zero.

cell.btn.isHidden = true
0
Litle Dev On

When you are making the height of the cell is 0 then, the subviews remains their height as assigned before. So, the subviews also have to remove from the cell or have to make their alpha 0.

try like:

cell.contentView.removeFromSuperview()

or,

cell.contentView.alpha = 0

or,

Make alpha zero (or remove) the subviews of the cell by taking them individually.