UITableView HeaderView disappears on Tableview beginupdates

Hi,


I have created custom headers which has buttons which will be used across the entire app

Also in the tableview we have textviews so i need to resize and scroll

When i do that all the headerviews go blank


For headerview i am passing a uitableviewcell, since it needs a view



    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        guard let cell1 = tableView.dequeueReusableCell(withIdentifier: "header") as? CustomCell else {
            return UITableViewCell()
        }
        cell1.delegate = self
        cell1.btnHeader.setTitle("Test", for: .normal)
     
        return cell1




On textview did change i call the following code


        let size = textView.bounds.size
        let newSize = textView.sizeThatFits(CGSize(width: size.width, height: CGFloat.greatestFiniteMagnitude))
        /
        if size.height != newSize.height {
            UIView.setAnimationsEnabled(false)
            tableView?.beginUpdates()
            tableView?.endUpdates()
            UIView.setAnimationsEnabled(false)
           guard let cell = textView.superview?.superview?.superview as? CustomCell else {
                return
            }
            if let thisIndexPath = tableView?.indexPath(for: cell) {
                tableView?.scrollToRow(at: thisIndexPath, at: .bottom, animated: false)
            }
        }

Accepted Reply

This question came up in a different context just a few days ago (https://forums.developer.apple.com/thread/97072). You should not attempt to use a UITableViewCell for something that's not a cell (i.e. doesn't have a row index), and you especially shouldn't try using a dequeued cell. The documentation (https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614901-tableview) says:


The returned object can be a UILabel or UIImageView object, as well as a custom view.


You're going to have to use a custom view, since you need a label and a button.

Replies

This question came up in a different context just a few days ago (https://forums.developer.apple.com/thread/97072). You should not attempt to use a UITableViewCell for something that's not a cell (i.e. doesn't have a row index), and you especially shouldn't try using a dequeued cell. The documentation (https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614901-tableview) says:


The returned object can be a UILabel or UIImageView object, as well as a custom view.


You're going to have to use a custom view, since you need a label and a button.

Thank you..