简体   繁体   English

从底部切割的 UITableViewCell 内部的 UITableView

[英]UITableView inside UITableViewCell cutting from the bottom

I've a vertical listing with 7 types of UITableViewCells.我有一个包含 7 种 UITableViewCells 的垂直列表。 One of them consist a UITableView inside the cell.其中一个在单元内包含一个 UITableView。

My requirement is the main tableview should autoresize the cell according to the cell's inner tableview contentSize.我的要求是主 tableview 应该根据单元格的内部 tableview contentSize 自动调整单元格的大小。 That os the inner tableview will show its full length and the scrolling will be off.那个 os 内部的 tableview 将显示它的全长并且滚动将被关闭。

This approach working fine, but for cell with tableview only.这种方法工作正常,但仅适用于带有 tableview 的单元格。 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.当我在内部 tableview 上方引入 UIImageView(带有异步加载图像)时,单元格的总高度略小于其内容的实际高度。 And so the inner tableview is getting cut from bottom.因此,内部 tableview 正在从底部切开。

Here is a representation of the bug.这是错误的表示。

漏洞

I'm setting the height of UImageView according to the width to scale properly:我正在根据宽度设置 UImageView 的高度以正确缩放:

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:内表视图是 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.问题可能与您的异步加载图像有关,因此您应该预测图像大小并在图像尚未加载时为表格视图提供正确估计的行高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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