简体   繁体   中英

UITableView inside UITableViewCell cutting from the bottom

I've a vertical listing with 7 types of UITableViewCells. One of them consist a UITableView inside the cell.

My requirement is the main tableview should autoresize the cell according to the cell's inner tableview contentSize. That os the inner tableview will show its full length and the scrolling will be off.

This approach working fine, but for cell with tableview only. When I introduce a UIImageView (with async loading image) above inner tableview, the total height of cell is somewhat smaller than the actual height of its contents. And so the inner tableview is getting cut from bottom.

Here is a representation of the bug.

漏洞

I'm setting the height of UImageView according to the width to scale properly:

if let media = communityPost.media, media != "" {
                postImageView.sd_setImage(with: URL(string: media), placeholderImage: UIImage(named: "placeholder"), options: .highPriority) { (image, error, cache, url) in
                    if let image = image {
                        
                        let newWidth = self.postImageView.frame.width
                        
                        let scale = newWidth/image.size.width
                        let newHeight = image.size.height * scale
                        
                        if newHeight.isFinite && !newHeight.isNaN && newHeight != 0 {
                            self.postViewHeightConstraint.constant = newHeight
                        } else {
                            self.postViewHeightConstraint.constant = 0
                        }
                        
                        if let choices = communityPost.choices {
                            self.datasource = choices
                        }
                        self.tableView.reloadData()
                    }
                }
            } else {
                self.postViewHeightConstraint.constant = 0
                
                if let choices = communityPost.choices {
                    datasource = choices
                }
                tableView.reloadData()
            }

And the inner table view is a subclass of UITableView:

class PollTableView: UITableView {
    override var intrinsicContentSize: CGSize {
        self.layoutIfNeeded()
        return self.contentSize
    }

    override var contentSize: CGSize {
        didSet{
            self.invalidateIntrinsicContentSize()
        }
    }

    override func reloadData() {
        super.reloadData()
        self.invalidateIntrinsicContentSize()
    }
}

The main table view is set to resize with automaticDimension:


    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cellHeights[indexPath] = cell.frame.size.height
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return cellHeights[indexPath] ?? UITableView.automaticDimension
    }
    

Can't seem to understand what is going wrong. Any help is appreciated. Thanks.

When the table view is asking you to estimate the row height, you are calling back the table view. Thus you are not providing it with any information it doesn't already have. The problem is probably with your async loading image, so you should predict the image size and provide the table view with properly estimated row height when the image hasn't loaded yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM