首页 > 解决方案 > UITableVIew 无法正确渲染 UITableViewCell

问题描述

当我要用不同的状态填充我的 tableView 时,一种状态会出错,我确信它是由 tableView 的延迟引起的。请先看视频和图片。

我像这样填充我的 UITableView:

func tableView(_ tableView: UITableView,
              cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellId")
    cell.state = myStates[indexPath.row]
}

在我的UITableViewCell

var state: MyCustomDesignState? {
    didSet {
        self.button.set(state: self.state)
    }
}

在我的UIButton

func set(state: MyCustomDesignState?) {
    switch state { 
    case ...
    case .blackBack:
        self.backgroundColor = background
        self.setTitleColor(textColor, for: .normal)
        self.frame = CGRect.init(x: 40, y: 190 - 40, width: 180, height: 34)
        self.dropFlatShadow(color: #colorLiteral(red: 0.1008123383, green: 0.1008369699, blue: 0.1008091196, alpha: 1))
        self.clipsToBounds = true
        self.layer.cornerRadius = 5
    }
}

这就是我想要的,也是我得到的:

在此处输入图像描述

我的 tableView 看起来如何:

错误的视频:

https://streamable.com/ki7vx

标签: iosswiftuitableview

解决方案


在您的TableViewCell:尝试调用您当前在状态更改中调用的此方法,如下所示:

override func layoutSubviews() {
          super.layoutSubviews()
          self.button.set(state: self.state)
    }

希望我的问题是正确的,这对你有帮助。


推荐阅读