Reputation: 1598
I am loading two objects of the same type into two separate arrays. The first array is of type [Post]
, along with the second array. I have two sections in my collectionView and I want to load the first array into the first section and the second array into the second section. I'm fetching the data from firebase and appending the object into the array. In my numberOfItemsInSection
I am checking if the section is equal to one, load the first and if the section is equal to two, load the second. This works fine but the issue is that it's only loading the correct amount of cells in each section but not loading the correct information. It just loads the previously fetched data from the first array. How do I get around this? Thank you...
class ProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var name: String?
var posts = [Post]()
var repost = [Post]()
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.register(PostCell.self, forCellWithReuseIdentifier: reuseIdentifier)
collectionView?.register(ProfileHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier)
collectionView?.register(RepostedHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: boostedHeaderIdentifier)
self.posts.removeAll()
self.repost.removeAll()
handleFetchUserPosts { self.collectionView?.reloadData() }
handleFetchUserReposts { self.collectionView?.reloadData() }
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
return posts.count
} else {
return reposts.count
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
if section == 0 {
return CGSize(width: view.frame.width, height: 180)
} else {
return CGSize(width: view.frame.width, height: 30)
}
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if indexPath.section == 0 {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier, for: indexPath) as! ProfileHeader
header.profileController = self
return header
} else {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: boostedHeaderIdentifier, for: indexPath) as! RepostedHeader
return header
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: width, height: height)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PostCell
cell.profileController = self
let posts = self.posts[indexPath.item]
let reposts = self.reposts[indexPath.item]
if self.posts != [] {
cell.post = posts
}
if self.posts != [] {
cell.post = reposts
}
return cell
}
}
Upvotes: 1
Views: 911
Reputation: 627
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PostCell
cell.profileController = self
if indexPath.section == 0 {
let posts = self.posts[indexPath.item]
if self.posts != [] {
cell.post = posts
}
}
if indexPath.section == 1 {
let reposts = self.reposts[indexPath.item]
if self.posts != [] {
cell.post = reposts
}
}
return cell
}
I think this will solve your problem if not use this, actually this is kind of same code but if you have multiple cell for your objects this will come handy
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell = PostCell()
if indexPath.section == 0 {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PostCell
cell.profileController = self
let posts = self.posts[indexPath.item]
if self.posts != [] {
cell.post = posts
}
}
if indexPath.section == 1 {
cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PostCell
let reposts = self.reposts[indexPath.item]
if self.posts != [] {
cell.post = reposts
}
}
cell.profileController = self
return cell
}
hope this will help
Upvotes: 2