0

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:

Overlapping value while for a blink of an eye

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!

}

Christian
  • 75
  • 10

1 Answers1

1

I don't have enough information about your code but this problem is something that

prepareForReuse()

can fix.

Performs any clean up necessary to prepare the view for use again.

Override this function in you custom cell class, and clean up any UILabel, You have this function will trigger every time a cell is reloaded or dequeued.

an example of what are you going to do.

override func prepareForReuse() {
    super.prepareForReuse()
    label.text = "" // cleans up the text inside the label to reuse again 
} 

UPDATE: if the above did not work,

you probably need to clear out your data source before calling reloadData.

The reason you're having this effect is that the source that you use to determine the number of items and sections in the collection view still has old data in it.

A simple way to verify this is to NSLog the contents of the data source just before calling reloadData.

OP

Hope this helps!

Mohmmad S
  • 5,001
  • 4
  • 18
  • 50
  • I have tested it, but no difference. The prepareForReuse() is also only for the cells which are not in the view. its like when scrolling through the collectionView and some cells are not in the visible view, than the method prepareForReuse() is throwing for that special cell which is not in the visible view. – Christian Oct 24 '18 at 07:13
  • provide more code in the question so we . can take a look into it – Mohmmad S Oct 24 '18 at 07:14
  • Updated my Code – Christian Oct 24 '18 at 07:22
  • The Datasource is the same as before and after reloadItems(). The only difference is the one value: "Kilometerstand". With reloadItems() I update only that specific cell with where the old value of "Kilometerstand" exists. So that's not the problem. The Overlapping on my label is only for a millisecond, you only can see it when taking a screen recording from that app and pause it when the value is changing. – Christian Oct 24 '18 at 07:55