UICollectionViewController - sometimes shows the wrong cells during fast scrolling

I have a UICollectionViewController and my custom cells, and in my method cellForRowAtIndexPath: I set the cells based on indexPath.row. But I get the wrong results, this cell appears even after the first position, and if you scroll back and forth, it appears in random places. How to fix it?

Here is the code:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { DVGCollectionViewCell *cell; cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; if (indexPath.row == 0) { cell.imageView.image = [UIImage imageNamed:@"something1.png"]; cell.buyLabel.text = @"170"; cell.textLabel.text = @"11.2011"; } return cell; } 
+4
source share
4 answers

I found the problem, once the contents of the cell were installed, it was never cleared. Therefore, I added cleaning properties of each cell as an additional sentence, and it works great.

+1
source

The cell is processed in both the UITableView and the UICollectionView, which means that when you exit the screen, it is placed in the NSSet until you need it. When needed, it is removed from the creative set added again to the UICollectionView hierarchy. If you do not clear the value inside the cell or set them again, the cell will show the same data when it was created.
This is done for performance reasons, so creating a cell takes longer, not a value again.
If your layout problem checks the layout flow object, what size have you set?

+4
source

You can perform any cleanup necessary to reuse the view to use if you override prepareForReuse in your custom cell implementation.

+1
source

One of the answers in this SO post helped: override prepareForReuse and reset the cell to the default state. Remember to call super.prepareForReuse .

0
source

Source: https://habr.com/ru/post/1488840/


All Articles