Can´t add items to UICollectionView inside UIView

2020-06-01 06:12发布

Objective

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

tableView.tableHeaderView = BusinessViewTableHeader.instanceFromNib() as! BusinessViewTableHeader

Inside BusinessViewTableHeader there is a UICollectionView which are supposed to display images when swiped, much like the Tinder app.

This is my UIView subclass:

class BusinessViewTableHeader: UIView {

    @IBOutlet var collectionView: UICollectionView!

    override func awakeFromNib() {
        super.awakeFromNib()
        self.collectionView.delegate = self
        self.collectionView.registerNib(UINib(nibName: "BusinessImageCollectionCell", bundle: nil), forCellWithReuseIdentifier: "BusinessImageCollectionCell")
    }

    class func instanceFromNib() -> UIView {
        return UINib(nibName: "BusinessViewTableHeader", bundle: nil).instantiateWithOwner(nil, options: nil)[0] as! UIView
    }

    ....
}

extension BusinessViewTableHeader: UICollectionViewDelegate, UICollectionViewDataSource {
    ....
}

Problem

I have a custom UIView xib containing a UICollectionView. The problem is that I can´t add any cells (items) to the UICollectionView. I can add items to my other UICollectionView which are placed inside a UIViewController. The first image is showing the properties for the UICollectionView inside a UIViewController, the second image is showing the UICollectionView inside a UIView xib.

UICollectionView in UIViewController [UICollectionView in UIView xib2

Question

Why are I not able to add items to the UICollectionView inside the UIView xib? Is there a way of doing this?

5条回答
The star\"
2楼-- · 2020-06-01 06:51

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!)
        }

    }
查看更多
女痞
3楼-- · 2020-06-01 06:53

Adding cells in a xib is not supported. If you must use a xib file, then you will need a separate xib which contains the UICollectionView cell. Storyboards may be a better solution.

It is not clear what you are trying to achieve. UICollectionView has specific means for creating headers which uses the datasource and delegate. Collection views are good for displaying items in a grid layout or other complex arrangements.

If all you need is to display a list of rows, then a UITableViewController might be an easier alternative.

Whatever the case, it is probably better to use a storyboard instead of a xib, and to subclass the UICollectionViewController or UITableViewController, rather than a subview.

Your custom class name can be entered in the identity inspector for the UIViewController or UIView:

UIViewController identity inspector

UIView identity inspector

查看更多
聊天终结者
4楼-- · 2020-06-01 06:55

In Swift 3.0 register nib with following method-

let nibName = UINib(nibName: "FruitCell", bundle:nil)
collectionView.register(nibName, forCellWithReuseIdentifier: "CellIdentifier")
查看更多
\"骚年 ilove
5楼-- · 2020-06-01 06:58

Same as @Pato's answer, but here is a more thorough tutorial for how to add a customized UICollectionViewCell inside a Xib file from @aestusLabs on Medium. It's a 3-5 min reading, I personally find it very helpful. It basically tells you to create another customized UICollectionViewCell with .xib, and register it in your "level 1" cell's awakeFromNib().

https://medium.com/@aestusLabs/adding-a-uicollectionviews-to-a-custom-uitableviewcell-xib-tutorial-swift-4-xcode-9-2-1ec9ce4095d3

查看更多
女痞
6楼-- · 2020-06-01 07:15

You can't have UICollectionViewCell when the UICollectionView is on a Nib. What you need to do is to create the UICollectionViewCell as another nib and get it registered in the class that you are using for your CollectionView.

Create a new nib, drag a UICollectionViewCell inside it, and do something like this in the class that works with your UICollectionView.

override func awakeFromNib() {
    let nibName = UINib(nibName: "ClassCollectionCell", bundle:nil)
    collectionView.registerNib(nibName, forCellWithReuseIdentifier: "collectionCell")
}

Remember you can add a custom class to the UICollectionViewCell so you can pass dynamic data to it.

查看更多
登录 后发表回答