dang
dang

Reputation: 2422

How to remove and add UICollectionView from/to parent view without loading the previous data?

Removing and adding Collection view again with new data but still showing the old data. (Don't need reloading, just need to remove and add collection view again with new data)

Description: The issue goes as follows:

This is exactly what I am doing in swift:

func onSwitchingPerspective(url: String) {

headerArray.removeAll() // REMOVED THE HEADER AND CONTENT ARRAY
contentArray.removeAll()

self.customHeaderCollectionView.removeFromSuperview() // REMOVED THE HEADER AND CONTENT COLLECTION VIEWS
self.customContentCollectionView.removeFromSuperview()


DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async { // NEED GLOBAL QUEUE TO CALL API AND LATER UPDATE UI ON MAIN THREAD

(self.headerArray, self.contentArray) = self.getEventsData(URL: url)

DispatchQueue.main.async {

self.showHeaderCollectionView() // ADDED THE COLLECTION VIEWS AGAIN
self.showContentCollectionView()

self.customContentCollectionView.collectionViewLayout.invalidateLayout() // CODE TO RELOAD BOTH THE COLLECTION VIEWS
self.customContentCollectionView.reloadData()
self.customContentCollectionView.scrollToItem(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
self.customHeaderCollectionView.collectionViewLayout.invalidateLayout()
self.customHeaderCollectionView.reloadData()
self.customHeaderCollectionView.scrollToItem(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
}
}
}

Upvotes: 0

Views: 446

Answers (1)

iAj
iAj

Reputation: 3817

Without

self.collectionView.reloadData()

it shows only your previous data.. Its necessary to reload after your

headerArray.removeAll() // REMOVED THE HEADER AND CONTENT ARRAY
contentArray.removeAll()

Upvotes: 0

Related Questions