Đorđe Nilović
Đorđe Nilović

Reputation: 3739

What is the way to load a fixed number of UICollectionViewCells without reuseIdentifier ? (Swift)

I have 5 columns in UICollectionView (horizontal scroll) and everything is working fine.

Every UICollectionViewCell contains its own UITableView inside and sometimes there is a lot of data in it, so the UICollectionView scroll is not so smooth.

I realized that the main problem is reusing the cell since the problem occurs only when the table contains a lot of data (10+ rows). Since the data is not updated so often and there is really no need to reuse it while scrolling, I believe that this will fix the 'smooth scroll' problem.

One of the solutions could be to add pagination to UITableView, so I can keep reuseIdentifier and not think about memory, but I just wanted to check this way as well.

Currently, I'm using this:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCustomIdentifier", for: indexPath) as? MyCustomCell {
        // Cell configuration code
        return cell
    }

    return UICollectionViewCell()
}

I tried with something like this:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if let cell = UICollectionViewCell() as? MyCustomCell {
        // Cell configuration code
        return cell
    }

    return UICollectionViewCell()
}

but every time when I remove reuseIdentifier it's giving me this error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the cell returned from -collectionView:cellForItemAtIndexPath: does not have a reuseIdentifier - cells must be retrieved by calling -dequeueReusableCellWithReuseIdentifier:forIndexPath:'

Upvotes: 0

Views: 77

Answers (0)

Related Questions