Can´t add items to UICollectionView inside UIView xib

后端 未结 5 1768
说谎
说谎 2021-02-05 09:20

Objective

I wanna place my (BusinessViewTableHeader: UIView) as tableView header:

tableView.tableHeaderView = BusinessViewTableHeader.instanceFromNib() a         


        
5条回答
  •  一整个雨季
    2021-02-05 09:36

    In Swift 3.0

    first Create Xibfile of UIView

    import UIKit
    
    class SubCatagoryListView:UIView , UICollectionViewDelegate , UICollectionViewDataSource
    {
        @IBOutlet weak var mainView: UIView!
        @IBOutlet weak var btnClose: UIButton!
        @IBOutlet weak var subCategoryListCollectionView: UICollectionView!
        @IBOutlet weak var lblTitle: UILabel!
    
        override func awakeFromNib() {
            super.awakeFromNib()
            subCategoryListCollectionView.register(UINib(nibName: "SubcatagoryListCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "SubcatagoryListCollectionViewCell")
            mainView.layer.cornerRadius = 10.0
            mainView.clipsToBounds = true
        }
        static func subCatagoryListView() -> SubCatagoryListView? {
            let arr = Bundle.main.loadNibNamed("SubCatagoryListView", owner: self, options: nil)
            if arr != nil {
                if arr!.count > 0 {
                    if let view = arr![0] as? SubCatagoryListView {
    
                        return view;
                    }
                }
            }
    
            return nil;
        }
    
    
    
    
    
        @IBAction func btnBackgroundTapped(_ sender: UIButton)
        {
             self.removeFromSuperview()
        }
        @IBAction func btnCloseTapped(_ sender: UIButton)
        {
            self.removeFromSuperview()
        }
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return 10
        }
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SubcatagoryListCollectionViewCell", for: indexPath) as! SubcatagoryListCollectionViewCell
    
            return cell
        }
        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
            let cellsize = CGSize(width: (subCategoryListCollectionView.bounds.size.width/3) - 10, height: 50)
            return cellsize
        }
    
    
    }
    

    After Create new CollectionViewCell Xib file

    import UIKit
    
    class SubcatagoryListCollectionViewCell: UICollectionViewCell {
    
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
    
    }
    

    after create both file i am load xib on my storybord

    var subcatagoryXib:SubCatagoryListView?
    
    override func awakeFromNib() {
            super.awakeFromNib()
            if let subcategoryView = SubCatagoryListView.subCatagoryListView()
            {
    
                subcatagoryXib = subcategoryView
            }
    
        }
     @IBAction func btnAddInterestTapped(_ sender: UIButton) {
    
            if subcatagoryXib != nil
            {
                self.subcatagoryXib!.frame = CGRect(x:0, y: 0, width: self.view.frame.width , height: self.view.frame.height)
                self.view.addSubview(self.subcatagoryXib!)
            }
    
        }
    

提交回复
热议问题