Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable height UITableViewCell expands after scrolling to end of list and rotating

UPDATE

I know what is causing this strange breaking of layout. It is the setting of an Accessory (UITableViewCellAccessory). If I stop specifying an accessory the layout does not break. I have not added this as the answer because an answer would need a solution that gives me an accessory without breaking layout

Most of the issues I see people having with custom cells of dynamic height are that they do not have the correct height until they are rotated. However I see the opposite: All cells are the height valid for their dynamic content. Scrolling up and down does not break this. However if I scroll to the bottom of the list, then rotate the device, then rotate back one row will become between 0.5 and 1.5 times the height of the screen.

A further rotation or a further scroll will put the rows back to the expect height. I have included a couple of before and after screenshots

Table before scroll

Table after scroll and rotate

The UITableView is defines as follows

this.rootChildrenTable = new UITableView()
            {
                TranslatesAutoresizingMaskIntoConstraints = false,
                AccessibilityIdentifier = "rootChildrenTable",
                RowHeight = UITableView.AutomaticDimension,
                EstimatedRowHeight = 44.0f,
                BackgroundColor = UIColor.GroupTableViewBackgroundColor,
                TableFooterView = new UIView(),
                TableHeaderView = this.searchBar,
                KeyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag
            };

Note the usual suspects are set RowHeight and EstimatedRowHeight. As soon as I remove the Lines = 0 from the label, making the rows all the same height, the issue goes away.

Any idea what else I should be looking at?

like image 272
Pat Long - Munkii Yebee Avatar asked Sep 13 '16 09:09

Pat Long - Munkii Yebee


1 Answers

I had facing the same issue in my project. I had overcome that by the following way to set the height for the row.

 //calculate the height by this way
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.heightForBasicCell(at: indexPath)
    }


    func heightForBasicCell(at indexPath: IndexPath) -> CGFloat {
        var sizingCell: YourCell? = nil
        var onceToken: dispatch_once_t
        dispatch_once(onceToken, {() -> Void in
            sizingCell = tableView.dequeueReusableCell(withIdentifier: YourCell.cellIdentifier())!
        })
        self.configureBasicCell(sizingCell, at: indexPath)
        return self.calculateHeight(forConfiguredSizingCell: sizingCell)
    }

    func calculateHeight(forConfiguredSizingCell sizingCell: YourCell) -> CGFloat {
        sizingCell!.bounds = CGRect(x: 0.0, y: 0.0, width: CGRectGetWidth(tableView.frame), height: CGRectGetHeight(sizingCell!.bounds))
        sizingCell!.setNeedsLayout()
        sizingCell!.layoutIfNeeded()
        var size = sizingCell!.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
        return size.height + 1.0
        // Add 1.0f for the cell separator height
    }

    func configureBasicCell(_ cell: YourCell, at indexPath: IndexPath) {
//set your text here
    cell(“text”)
    }

Change code according to your need. I just converted objective C code to swift.

like image 125
Vishnuvardhan Avatar answered Nov 15 '22 03:11

Vishnuvardhan