如何在Collectionview标头中添加Instagram故事功能?

时间:2019-05-01 00:46:38

标签: swift uicollectionview uicollectionviewcell

我正在尝试通过使用collectionView来模仿instagram,其中一个单元格代表提要,而另一个collectionView则位于父级collectionView的标题中,但是不确定这是否是正确的方法。 任何建议或建议

到目前为止,我可以显示来自父collectionview的自定义单元格的数据,但是标题为collectionvew的标题为空白

父母CollectionVIewFeedVC

import UIKit

class FeedVC: UIViewController {

    var datta = ["hgfhhgh hfgh","erteeertrt","dfdfdfd","ssss"]
    var allImages = [UIImage(named: "pants"), UIImage(named: "shirt"), UIImage(named: "tesla"), UIImage(named: "skull"), UIImage(named: "avatar"), UIImage(named: "heart")]
     static var rever = [UIImage(named: "skull"), UIImage(named: "avatar"), UIImage(named: "heart"), UIImage(named: "hart"), UIImage(named: "heart"), UIImage(named: "tesla")]

    lazy var navSearchBar = UISearchBar()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
//        var b = UIBarButtonItem.menuButton(self, action: #selector(test), imageName: "avatar")
//        navigationItem.leftBarButtonItem = b
    }

    @IBOutlet weak var profileNavButtonImage: UINavigationItem!
    @IBOutlet weak var searchOutlet: UIBarButtonItem!    

    @IBAction func SearchButtonPressed(_ sender: UIBarButtonItem) {
        let sb = storyboard?.instantiateViewController(withIdentifier: "SearchVCID") as! SearchVC

        present(sb, animated: true, completion: nil)
    }

    @objc func test(){
        print("pressed")
    }

    @IBAction func calendarPopUp(_ sender: UIButton) {
        print("uop")
        let sb = storyboard?.instantiateViewController(withIdentifier: "CalendarPopUpID") as! CalendarPopUp
        present(sb, animated: true, completion: nil)
    }
}

extension FeedVC: UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource{

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return allImages.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "feedCell", for: indexPath) as! FeedCell

        cell.userPost.image = FeedVC.rever[indexPath.row]
        cell.profileImageOutlet.image = allImages[indexPath.row]

        return cell
    }
}


extension FeedVC: UINavigationBarDelegate, UISearchBarDelegate{

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {

    }
}

CollectionViewFeedVC中基本单元格的自定义单元格

import UIKit

class FeedCell: UICollectionViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()


        profileImageOutlet.layer.cornerRadius = profileImageOutlet.frame.width / 2
    }

    //MARK: Profile Image
    @IBOutlet weak var profileImageOutlet: UIImageView!

    //User Post Button Image
    @IBOutlet weak var userPost: UIImageView!

//    //Like Button Image
//    @IBOutlet weak var likeButtonOutlet: UIImageView!
//
//    //Comment Button Image
//    @IBOutlet weak var commentButtonOutlet: UIImageView!
    var stories = [UIImage(named: "skull"), UIImage(named: "avatar"), UIImage(named: "heart"), UIImage(named: "hart"), UIImage(named: "heart"), UIImage(named: "tesla")]

}

extension CollectionViewCell: UICollectionViewDataSource, UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return FeedVC.rever.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "storiesCell", for: indexPath) as! StoriesCell
        cell.storiesPic.image = FeedVC.rever[indexPath.row]
        return cell
    }
}

页眉中Collectionview的故事单元格

import UIKit

class StoriesCell: UICollectionViewCell {
    override func awakeFromNib() {
        super.awakeFromNib()

        storiesPic.layer.cornerRadius = storiesPic.frame.height / 2
    }
    @IBOutlet weak var storiesPic: UIImageView!
}

0 个答案:

没有答案