【发布时间】:2018-10-24 06:09:44
【问题描述】:
我有一个带有原型单元的 CollectionView。单元格有一个标签。标签经常随时间改变值。我用collectionView.reloadItems[at: [indexPath]] 重新加载数据,我的问题是当我使用此方法更新集合视图中的标签时,当标签正在更新他的值时,我眨眼之间就会重叠值:如图所示:
当我使用collectionView.reloadData() 方法时,我没有这个问题。但是更新集合视图需要更多的 CPU 能力和时间。
更多代码: 在这里我收集单元格的新数据并将其发送到我的 updateCell 函数:
let btesMeter:[UInt8] = [data![7], data![6], data![5], data![4]]
let resultMeter = (UInt32(btesMeter[3]) << 24) + (UInt32(btesMeter[2]) << 16) + (UInt32(btesMeter[1]) << 8) + UInt32(btesMeter[0])
let tempresultMeter = resultMeter / 1000
updateCell(cellname: "Kilometerstand", newValue: String(tempresultMeter))
这是我的 UpdateCel 函数:
func updateCell(cellname: String, newValue: String) {
let cell = cellname
if let cellname = periodicData.first(where: {$0.title == cell}) {
if cellname.value == newValue {
return
} else {
cellname.value = newValue
if let indexOf = periodicData.firstIndex(where: {$0.title == cell}) {
// Reload the index
let indexPath = IndexPath(item: indexOf, section: 0)
let array:[IndexPath] = [indexPath]
collectionView.reloadItems(at: array)
}
}
}
}
cellForItemAt:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCollectionViewCell
cell.labelTitle.text = periodicData[indexPath.row].title
cell.labelValue.text = periodicData[indexPath.row].value
return cell
}
CustomCollectionViewCell:
class CustomCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var labelTitle: UILabel!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var labelValue: UILabel!
}
【问题讨论】:
标签: swift label collectionview reloaddata