当我调用reload tableView.reloadData()时,自定义单元格属性不会发生变化

时间:2018-06-09 04:19:03

标签: swift uitableview realm

我有一个自定义单元格的表视图,数据来自领域:

var Materials: Results<RealmMaterial>!

override func viewDidLoad() {
    let realm = RealmService.shared.realm
    Materials = realm.objects(RealmMaterial.self).filter("InspectResult == 0")
}

在每个单元格中,我有一些属性随单元格信息而变化的按钮。我这样处理:

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCell(withIdentifier: "MaterialCell") as! MaterialsTableViewCell
    let myMaterial = Materials[indexPath.row]
    cell.materialCodeLbl.text = myMaterial.Number
    cell.materialDescLbl.text = myMaterial.Desc
    cell.redBtnOutlet.tag = myMaterial.Id
    cell.greenBtnOutlet.tag = myMaterial.Id
    cell.yellowBtnOutlet.tag = myMaterial.Id
    cell.cellView.layer.shadowColor = UIColor.black.cgColor
    cell.cellView.layer.shadowOpacity = 0.1
    cell.cellView.layer.shadowOffset = CGSize.zero
    cell.cellView.layer.shadowRadius = 10
    cell.cellView.layer.cornerRadius = 10
    cell.redBtnOutlet.layer.cornerRadius = 5
    cell.greenBtnOutlet.layer.cornerRadius = 5
    cell.yellowBtnOutlet.layer.cornerRadius = 5
    if myMaterial.InspectResult == 1 {
        cell.greenBtnOutlet.backgroundColor = UIColor(red: 137/255, green: 206/255, blue: 75/255, alpha: 1)
        cell.yellowBtnOutlet.backgroundColor = UIColor(red: 192/255, green: 192/255, blue: 192/255, alpha: 1)
        cell.redBtnOutlet.backgroundColor = UIColor(red: 192/255, green: 192/255, blue: 192/255, alpha: 1)
    }
    else if myMaterial.InspectResult == 2 {
        cell.redBtnOutlet.backgroundColor = UIColor(red: 253/255, green: 77/255, blue: 67/255, alpha: 1)
        cell.greenBtnOutlet.backgroundColor = UIColor(red: 192/255, green: 192/255, blue: 192/255, alpha: 1)
        cell.yellowBtnOutlet.backgroundColor = UIColor(red: 192/255, green: 192/255, blue: 192/255, alpha: 1)
    }
    else if myMaterial.InspectResult == 3 {
        cell.yellowBtnOutlet.backgroundColor = UIColor(red: 240/255, green: 192/255, blue: 16/255, alpha: 1)
        cell.greenBtnOutlet.backgroundColor = UIColor(red: 192/255, green: 192/255, blue: 192/255, alpha: 1)
        cell.redBtnOutlet.backgroundColor = UIColor(red: 192/255, green: 192/255, blue: 192/255, alpha: 1)
    }
    return cell

}

但是当我重新加载数据时,对象属性不会发生变化。接缝cellForRowAt不会再次执行,属性来自单元格索引。 这是第一次加载数据,它运行正常。但是当我重新加载数据时,它不会再次使用新数据进行更改

1 个答案:

答案 0 :(得分:0)

听起来我正在重新加载表视图,但实际上没有更新数据。

来自你的陈述

  

它接连了cellForRowAt不再执行

我想你实际上并没有确认cellForRowAt没有被执行,我的猜测是它正在被执行,但Materials数组还没有更新你的新数据。

在致电Materials之前,请确保tableView.reloadData()拥有新数据。

相关问题