我正在尝试为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)
任何想法我在这里做错了什么,如何获得自定义单元格的真实宽度,在我看来应该等于将要放置在其中的表格的宽度?
答案 0 :(得分:0)
将约束基于框架。使用适当的约束。
更改:
colu1 colu2
red apple red type
yellow banana type of fruits
green lettuce not a fruit
收件人:
col1 col2 col3
red apple red type is the type of fruits
red apple red type yellow type of fruits
yellow banana type of fruits is the type of fruits
yellow banana type of fruits yellow type of fruits