【发布时间】:2018-06-09 04:19:02
【问题描述】:
我有一个带有自定义单元格的表格视图,数据来自领域:
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
}
但是当我 reloadData 单元格的对象属性不会改变。它接缝cellForRowAt 不会再次执行,并且属性来自单元格索引。
第一次加载数据,它工作正常。但是当我重新加载数据时,它不会再用新数据改变
【问题讨论】:
-
cell.yellowBtnOutlet.backgroundColor 你的意思是颜色没有改变?
-
@iOSGeek 首次加载数据时工作正常。但是当我重新加载数据时,它不会再用新数据改变
-
你能显示你在何时何地重新加载 TableView 吗?
-
@iOSGeek 我有一个 uiswitch,当它发生变化时,我想从领域中过滤数据:
Materials = realm.objects(RealmMaterial.self).filter("InspectResult == 0") self.tableView.reloadData() -
您是否在更改开关时检查过数据是否已过滤? 和 是否使用更新的数据或以前的数据调用 cellForRow 尝试一件事 在 MainThread 上重新加载 TableView DispatchQueue.main.async { tableView.reloadData() }
标签: swift uitableview realm