One of two collection views is not displayed.

I tried to diagnose the problem here, but I really can not understand what went wrong. I got a view of the collection (messages), until I added an additional view of the collections in the navigation bar.

This is the setting for the second collection view (navigation bar 1):

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 methods of viewing the collection 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 a few statements are added if/elseto accommodate both views of the collection. Also this CV method, which is intended for structure JSQMessages(in other words, does not affect the second type of collection, only messages):

override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageDataForItemAt indexPath: IndexPath!) -> JSQMessageData! {
    return messages[indexPath.item]
}

And I added this one, which is necessary for the collection of navigation bars:

override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: CGFloat(40), height: CGFloat(40))
}

, . messages, , .

(, if/else s), , , , . .

- , ? , JSQMessages . , , , , collectionView, , if collectionView == navBarCollectionView, , ? . , , :

enter image description here

, - , , ! .

+4

Source: https://habr.com/ru/post/1674979/


All Articles