Swift 如何在UITableViewCell类中访问正确的单元格宽度?

Swift 如何在UITableViewCell类中访问正确的单元格宽度?,swift,uitableview,layout,constraints,Swift,Uitableview,Layout,Constraints,我正在尝试获取UITableViewCell类中自定义单元格的正确宽度。我尝试了以下方法: import UIKit import ChameleonFramework class IsectionsCustomTableViewCell: UITableViewCell { let cellContainer: UIView = { let container = UIView() contain

我正在尝试获取UITableViewCell类中自定义单元格的正确宽度。我尝试了以下方法:

    import UIKit

    import ChameleonFramework

    class IsectionsCustomTableViewCell: UITableViewCell {

        let cellContainer: UIView = {

            let container = UIView()

            container.backgroundColor = UIColor.flatPink()

            container.translatesAutoresizingMaskIntoConstraints = false

            return container

        }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {

            super.init(style: style, reuseIdentifier: reuseIdentifier)

            addSubview(cellContainer)

    }

        required init?(coder aDecoder: NSCoder) {

            fatalError("init(coder:) has not been implemented")

        }

    func applyAppropriateSizeAndConstraintsForCellItems() {

            NSLayoutConstraint.activate([

                cellContainer.topAnchor.constraint(equalTo: self.topAnchor),

                cellContainer.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -1*((self.frame.size.width)/2)),

                cellContainer.bottomAnchor.constraint(equalTo: self.bottomAnchor),

                cellContainer.leftAnchor.constraint(equalTo: self.leftAnchor)

])
    }

    }
但是,从所附的图像中可以看出,上面使用的以下代码行没有返回正确的宽度,因为总宽度没有平均分成两半:

-1*((self.frame.size.width)/2)
你知道我做错了什么吗?我怎样才能得到自定义单元格的真实宽度,在我看来,这个宽度应该等于它要放在里面的桌子的宽度

问候,,
Shadi Hammoudeh.

将约束基于帧。使用适当的约束

更改:

cellContainer.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -1*((self.frame.size.width)/2))
致:


不要将约束基于框架。使用适当的约束

更改:

cellContainer.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -1*((self.frame.size.width)/2))
致:


非常感谢这解决了我的问题,非常感谢:)非常感谢这解决了我的问题,非常感谢:)