Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch updates #371

Open
ArtSp opened this issue Sep 22, 2020 · 0 comments
Open

Batch updates #371

ArtSp opened this issue Sep 22, 2020 · 0 comments

Comments

@ArtSp
Copy link

ArtSp commented Sep 22, 2020

When using custom UICollectionViewLayout for UICollectionView, it is possible that some cells have no layout attributes. When SectionedViewType inserts/moves cell at indexPath after datasource changed, cells without layout attributes being loaded and crash when func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) was called.

Walk-around - Use subclassed collection view which will check if it can insert/move/delete cell :

class BatchUpdateSafeCollectionView: UICollectionView {
    
    override func insertItems(at indexPaths: [IndexPath]) {
        let safeIndexPaths = indexPaths.filter { collectionViewLayout.layoutAttributesForItem(at: $0) != nil }
        super.insertItems(at: safeIndexPaths)
    }
    
    override func deleteItems(at indexPaths: [IndexPath]) {
       ...
    }
    
    override func moveItem(at indexPath: IndexPath, to newIndexPath: IndexPath) {
        ...
    }
}

Solution is not perfect as collection view will ignore invalid batch updates (will use reloadData() instead) as number of inserted/deleted/moved items may not be equal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant