当我使用 UITableViewCell 来自定义每个单元格内容时,在 Swift4 中滚动后屏幕布局被破坏



我想在UITableviewCell中创建一个自定义单元格,以便我可以更改单元格内容以及从ModelView传递的参数。但是,当我滚动时,布局被破坏了。

我试图将代码分为两部分,如下所示。


声明变量部分


初始化部件包含布局定义


但是如果我分成 2 部分,我无法更改布局和变量,因为 Init Part 首先在"声明变量部分"之前运行。此外,我尝试更改"声明变量部分"中的布局,布局已损坏。我应该如何编码以传递参数以更改单元格内容?下面是我代码的一部分。"baseView"的布局将被打破。

[ViewModel]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let item = items[indexPath.section]
    if let item = item as? AnniversaryViewModelContentsItem, let cell = tableView.dequeueReusableCell(withIdentifier: ANContentCell.identifier, for: indexPath) as? ANContentCell {
        cell.selectionStyle = .none
        cell.item = item.contents[indexPath.row]
        cell.contentView.setNeedsLayout()
        cell.contentView.layoutIfNeeded()
        return cell
    }
    return UITableViewCell()
}
[Cell: ANContentCell]
    // Declare Variable Part ---------------------------------------------------------------------
   ........
   var item: ANContent? {
        didSet {
            guard let item = item?.dataPerRow else {
                return
            }
            let graphArray = [graph00, graph01, graph02, graph03]
            if item.mode == 1 {
                baseView.anchor(
                    top: self.safeAreaLayoutGuide.topAnchor,
                    leading: self.safeAreaLayoutGuide.leadingAnchor,
                    bottom: self.safeAreaLayoutGuide.bottomAnchor,
                    trailing: self.safeAreaLayoutGuide.trailingAnchor,
                    padding: .init(top: 0, left:0, bottom:0, right:0), size: .init(width:0, height: item.hight )
                )
            } 
            for i in 0...(graphArray.count - 1){
                // Graph Anchor
                graphArray[i].anchor(
                    top: baseView.safeAreaLayoutGuide.topAnchor,
                    leading: baseView.leadingAnchor,
                    bottom: nil,
                    trailing: nil,
                    padding: .init(
                        top: item.top,
                        left: item.left,
                        bottom:0,
                        right:0
                    ),
                    size: .init(
                        width:item.width,
                        height: item.height
                    )
                )
                .........
            }
        }
    }
    private let baseView: UIView = {
        var baseView = UIView()
        baseView.backgroundColor = .clear
        return baseView
    }()
    var graph00: UIView = {
        let graph = UIView()
        return graph
    }()
    var graph01: UIView = {
        let graph = UIView()
        return graph
    }()
    var graph02: UIView = {
        let graph = UIView()
        return graph
    }()
    var graph03: UIView = {
        let graph = UIView()
        return graph
    }()
    // Init parts ---------------------------------------------------------------------
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        let graphArray = [graph00, graph01, graph02, graph03]
        // Add Object
        addSubview(baseView)
        for i in 0...(graphArray.count - 1){
            baseView.addSubview(graphArray[i])
        }
    }

尝试替换::

var graph00: UIView = {
        let graph = UIView()
        return graph
}()

自:

var graph00 = UIView()

并将此代码添加到您的单元格中:

override func layoutIfNeeded() {
        self.subviews.forEach{$0.layoutIfNeeded()}
}

最新更新