无法在UIView xib中向UICollectionView添加项目 [英] Can´t add items to UICollectionView inside UIView xib

查看:366
本文介绍了无法在UIView xib中向UICollectionView添加项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标



我想将我的(BusinessViewTableHeader:UIView)作为tableView标题放置:

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

在BusinessViewTableHeader内部有一个UICollectionView,它应该在刷卡时显示图像,就像Tinder app一样这是我的UIView子类:

 类BusinessViewTableHeader:UIView {

@IBOutlet var collectionView:UICollectionView!

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

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

....
}

扩展名BusinessViewTableHeader:UICollectionViewDelegate,UICollectionViewDataSource {
....
}



问题



我有自定义UIView xib包含一个UICollectionView。问题是我无法将任何单元格(项)添加到UICollectionView。我可以将项添加到放在UIViewController中的其他UICollectionView。第一个图像显示UIViewController内UICollectionView的属性,第二个图像显示UIView xib内的UICollectionView。





问题



为什么我无法在UIView xib内的UICollectionView中添加项目吗?有没有办法做到这一点?

解决方案

当UICollectionView在Nib上时,你不能拥有UICollectionViewCell。您需要做的是将UICollectionViewCell创建为另一个nib并将其注册到您用于CollectionView的类中。



创建一个新的笔尖,拖动一个其中包含UICollectionViewCell,并在与您的UICollectionView一起使用的类中执行类似的操作。

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

请记住,您可以向UICollectionViewCell添加自定义类,以便将动态数据传递给它。


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.

[2

Question

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

解决方案

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.

这篇关于无法在UIView xib中向UICollectionView添加项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆