ios - 如何更新单元格中的 View 框架

标签 ios swift tableview

我有一个单元格,里面有 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/

相关文章:

ios - Cordova 构建 ios 但不会运行

ios - 单击 iPhone 的 UItabButton 时如何通过 swift 重新加载网站页面

ios - Tableview - 根据 sqlite 中的数据创建 numberOfSectionsInTableView

ios - 表格仅在交互时显示,默认情况下不显示

ios - 为什么我收到字符串变量错误?

ios - 我以某种方式搞砸了 Xcode

ios - 为单个视频添加多个滤镜

ios - 无法使 : UICollectionElementKindCell 类型的 View 出队

ios - Swift OpenGL es 枚举

ios - 减小 UITableViewRowAction 的字体大小