繁体   English   中英

从底部切割的 UITableViewCell 内部的 UITableView

[英]UITableView inside UITableViewCell cutting from the bottom

我有一个包含 7 种 UITableViewCells 的垂直列表。 其中一个在单元内包含一个 UITableView。

我的要求是主 tableview 应该根据单元格的内部 tableview contentSize 自动调整单元格的大小。 那个 os 内部的 tableview 将显示它的全长并且滚动将被关闭。

这种方法工作正常,但仅适用于带有 tableview 的单元格。 当我在内部 tableview 上方引入 UIImageView(带有异步加载图像)时,单元格的总高度略小于其内容的实际高度。 因此,内部 tableview 正在从底部切开。

这是错误的表示。

漏洞

我正在根据宽度设置 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()
            }

内表视图是 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()
    }
}

主表视图设置为使用自动维度调整大小:


    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
    }
    

似乎无法理解出了什么问题。 任何帮助表示赞赏。 谢谢。

当表格视图要求您估计行高时,您正在回调表格视图。 因此,您没有向它提供它还没有的任何信息。 问题可能与您的异步加载图像有关,因此您应该预测图像大小并在图像尚未加载时为表格视图提供正确估计的行高。

暂无
暂无

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

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