ARTICLE AD BOX
I noticed that when I update the size of a table cell, the table cell below sometimes seems to disappear. Could you please tell me how to fix this? Here's an example of such a layout. This behavior sometimes goes unnoticed for quite a long time, and sometimes almost constantly. I thought it was related to the cell and simplified it as much as possible, but even in this case, you can catch this behavior. What could be the cause?
class ViewController: UIViewController { private var items: [String] = ["","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""] private lazy var tableView: UITableView = { let t = UITableView(frame: .zero, style: .plain) t.backgroundColor = .clear t.separatorStyle = .none t.estimatedRowHeight = 220 t.dataSource = self t.delegate = self t.register(ProductInfoAttributesCell.self, forCellReuseIdentifier: ProductInfoAttributesCell.reuseIdentifier) return t }() private var selectedIndex: [Int] = [] override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white view.addSubview(tableView) tableView.snp.makeConstraints { make in make.edges.equalTo(view.safeAreaLayoutGuide) } } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if selectedIndex.contains(indexPath.row) { return 200 } else { return 40 } } } extension ViewController: UITableViewDataSource, UITableViewDelegate { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { items.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell( withIdentifier: ProductInfoAttributesCell.reuseIdentifier, for: indexPath ) as! ProductInfoAttributesCell cell.onButtonTap = { [weak self] in self?.selectedIndex.append(indexPath.row) self?.tableView.performBatchUpdates(nil) } return cell } } // MARK: - ProductInfoAttributesCell final class ProductInfoAttributesCell: UITableViewCell { static let reuseIdentifier = "AccordionTableViewCell" var onButtonTap: (() -> Void)? let newContainerViewheight = 40 let newContainerViewheightMax = 90 let moreButtonheight = 20 // MARK: - Init override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) setupLayout() } @available(*, unavailable) required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } // MARK: Setup And Configuration private func setupLayout() { contentView.clipsToBounds = true contentView.backgroundColor = .red contentView.layer.borderWidth = 2 contentView.layer.borderColor = UIColor.black.cgColor let tap = UITapGestureRecognizer(target: self, action: #selector(headerContainerViewTapped)) contentView.addGestureRecognizer(tap) } @objc private func headerContainerViewTapped() { onButtonTap?() } }
