I have a CollectionView with a Prototype Cell. The cell has a label. The label change the value over the time very often. I reload the data with collectionView.reloadItems[at: [indexPath]]
, my problem is when I use this method updating the labels in the collection view, when the label is updating his value I have for a blink of an eye overlapping values: Like in the picture:
When I use the method collectionView.reloadData()
I don't have this issue. But it needs much more cpu power and time for updating the collection view.
More Code: Here I collect the new Data of the cell and send it to my updateCell function:
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))
This is my UpdateCel Function:
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!
}