I've been trying to diagnose the problem here but I really can't figure out what went wrong. I had a collection view (of messages) showing up fine, until I added an additional collection view in the navigation bar.
This is the setup for the second collection view (the nav bar one):
let navBarCollectionView: UICollectionView = UICollectionView(frame: CGRect(x: CGFloat(70), y: CGFloat(0), width: CGFloat(500), height: CGFloat(40)), collectionViewLayout: UICollectionViewFlowLayout.init())
viewDidLoad:
// Nav Bar collection view
let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
navBarCollectionView.setCollectionViewLayout(layout, animated: true)
navBarCollectionView.backgroundColor = UIColor.clear
navBarCollectionView.register(NavBarCell.self, forCellWithReuseIdentifier: "cell")
navBarCollectionView.delegate = self
navBarCollectionView.dataSource = self
layout.scrollDirection = .horizontal
self.navigationController?.navigationBar.addSubview(navBarCollectionView)
navBarCollectionView.reloadData()
Then, all I did was change the collection view methods from
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return messages.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell
let message = messages[indexPath.item]
if message.senderId == senderId {
cell.textView?.textColor = UIColor.white
} else {
cell.textView?.textColor = UIColor.black
}
return cell
}
to
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == navBarCollectionView {
return 30
} else {
return messages.count
}
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == navBarCollectionView {
let navBarCell = (collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)) as! NavBarCell
navBarCell.avatarImageView.loadImageUsingCacheWithUrlString("https://graph.facebook.com/*removed*/picture?type=large&return_ssl_resources=1")
navBarCell.avatarImageView.clipsToBounds = true
navBarCell.avatarImageView.layer.borderWidth = 1.5
navBarCell.avatarImageView.layer.borderColor = UIColor.getRandomColor().cgColor
navBarCell.avatarImageView.layer.cornerRadius = 20
return navBarCell
} else {
let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell
let message = messages[indexPath.item]
if message.senderId == senderId {
cell.textView?.textColor = UIColor.white
} else {
cell.textView?.textColor = UIColor.black
}
return cell
}
}
In other words, just added some if/else
statements to accommodate both collection views. There's also this CV method which is for the JSQMessages
framework (in other words does not affect the second collection view, only the messages):
override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageDataForItemAt indexPath: IndexPath!) -> JSQMessageData! {
return messages[indexPath.item]
}
And I added this one which is necessary for the nav bar collection:
override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: CGFloat(40), height: CGFloat(40))
}
With this code, the navigation bar collection view shows up fine, but the messages are no longer there. With print statements I can see the messages
and such do have data, they're just not showing.
If I revert back to using the original collection view methods (the ones without the if/else
s), the messages show up fine but obviously the navigation bar collection is no longer there. It's like one or the other.
Can anyone see what's going wrong here? I'm not sure if it's an issue specific with JSQMessages
or not. The only thing that seems like could be messing things up is that the messages collection view is just called collectionView
, so when I say if collectionView == navBarCollectionView
, maybe I'm re-assinging the variable? I really don't know. Here's a screenshot to show what I mean:
Anyway if anyone can see what's causing this, any help will be hugely appreciated! Thanks in advance.