我有一个单元格,里面有 1 个标签。
我想要什么时候向左然后向右,然后向左,然后向右显示这个标签...
这是我的代码:
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var titleData: UILabel!
var data: String? {
didSet {
titleData.text = data ?? ""
if alternativeMode {
titleData.frame.origin.x = (row % 2 != 0) ? 200 : 0
} else {
titleData.frame.origin.x = 200
}
layoutSubviews()
}
}
var row: Int!
var alternativeMode = false
override func layoutSubviews() {
super.layoutSubviews()
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
self.selectionStyle = .none
}
}
但是这个标签没有显示alternative。
最佳答案
我已经用另一种方法解决了这个问题
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var titleData: UILabel!
@IBOutlet weak var titleDataTrailingConstraint: NSLayoutConstraint!
var data: String? {
didSet {
titleData.text = data ?? ""
}
}
var row: Int!
var alternativeMode = false
override func layoutSubviews() {
super.layoutSubviews()
if titleDataTrailingConstraint != nil {
let screenSizeWidth = UIScreen.main.bounds.width
if alternativeMode {
titleDataTrailingConstraint.constant = (row % 2 == 0) ? (screenSizeWidth - titleData.bounds.width - 80) : 10
} else {
titleDataTrailingConstraint.constant = 10
}
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
self.selectionStyle = .none
}
}
关于ios - 如何更新单元格中的 View 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41208887/