0

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. 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?

  • Can you try to add `self.tableView.beginUpdates()`and `self.tableView.endUpdates()` after you remove the stack view and calculate your new height? – ronatory Mar 01 '17 at 12:03

3 Answers3

0

Try code for dynamic height, and Give constraint without fix height in tableView cell

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
    return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
    return 100.0
}
Ujesh
  • 1,698
  • 2
  • 23
  • 35
0

You should not be setting cell frames. That's not how TableViews work. If the cell heights are dynamic then @Theorist is correct. 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
} 
dmorrow
  • 5,152
  • 5
  • 20
  • 31
0

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)

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
}
muescha
  • 1,544
  • 2
  • 12
  • 22