S
S
Shakedown2022-01-28 18:37:53
iOS
Shakedown, 2022-01-28 18:37:53

Why are cells in UICollectionView changing?

I have a UICollectionView whose cells are configured based on data received from the web.

Here is the cellForItemAt method:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        print(indexPath.row)
            
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "searchingCell", for: indexPath) as! SearchingCollectionViewCell
        cell.configure(stringUrl: images[indexPath.row].fullUrl)
        
        return cell
    }

When I start flipping through my collection view, then the picture, for example, from cell 1 becomes cell 10. This happens because the method is called constantly when scrolling through the collection view, and different indexPaths may come. If I go to the beginning of the collection view, then for some reason a non-zero indexPath comes, that is, a picture from another cell appears on 1 cell. I've tried creating a cell array, populating it, and if the desired indexPath arrives, returning the cell. But the problem is that indexPath behaves very strangely. Can this be fixed in some proper way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
briahas, 2022-01-29
@Shakedown

Rethink your approach to filling cells with content. Those. or
- at the time of filling, the content should already be, and it is enough just to place it in the cell;
or
- (if the first option is not possible) fill the cell with "empty" content, and in parallel load the content asynchronously, but stop loading the content if the cell is out of the visible range.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question