In iOS 15.2 UITableView cell height bug when add embedded view in swift

398 views Asked by At

I have a project that works fine in xcode 12.5.1 and ios prior to 15.2 - it has UITableView with cells that contains embedded view. I set height of cells "not expanded" by default when loads UITableViewController and then expand them by tap on rows.

When I migrate to Xcode 13.2.1 and app works on ios 15.2 device it has a bug with cell height -

when UITableViewController loads and loads cells that contains embedded views then its height always set as a height of embedded view.

Maybe someone faced with this bug and have any solutions?

Code:

This is how I add cells -

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = cells[indexPath.section]![indexPath.row]
        cell.layer.zPosition = CGFloat(indexPath.row)
        cell.configure(with: models[indexPath.section]![indexPath.row])

        if indexPath.section == 0
        
        {
        switch indexPath.row {
        case 0:
            cell.addEmbeddedView(Section.init())
        default:
            break
        }
}

and this is how I "fix" size of cell to not expand before I tap on cell:

extension UITableView {
    func fixCellBounds() {
        DispatchQueue.main.async { [weak self] in
            for cell in self?.visibleCells ?? [] {
                cell.layer.masksToBounds = false
                cell.contentView.layer.masksToBounds = false
            }
        }
    }

and how I expand cell when tap on it:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let cell = tableView.cellForRow(at: indexPath) as? MyTableViewCell else { return }
        tableView.beginUpdates()
        cell.expanded.toggle()
        tableView.fixCellBounds()
        tableView.endUpdates()
    }


var expanded: Bool = false {
        didSet {
            let duration = CATransaction.animationDuration()
            UIView.animate(withDuration: duration) {
                self.arrow.transform = self.expanded ? CGAffineTransform(rotationAngle: .pi / 2) :  .identity
                
                self.height.isActive = !self.expanded
                self.foldView.alpha = self.expanded ? 1 : 0
                self.container.layoutIfNeeded()
            }
            
        }
    }
0

There are 0 answers