在UIView中用xib文件使用CollectionView

我正在做这个,我想使用CollectionView,但我还没有看到原型单元格,并不知道如何使用CollectionView在这种情况下,有人可以帮我吗?

我尝试像这样使用,但比UICollectionView花费很多时间和难于pipe理

在这里输入图像说明

使用UICollectionView的主要方法是通过编程pipe理逻辑。

  1. 首先,创build一个从UICollectionViewCellinheritance的新类。 select是否要包含一个xib来轻松devise您的单元格: 在这里输入图像说明

  2. 使用Interface Builder或以编程方式devise您的单元格。

  3. 创build你的主视图控制器包括一个xib(或一个故事板) 集合视图里面,并通过接口生成器链接到相关的类。 或者,你可以添加一个集合视图编程到你的UIViewController

在这里输入图像说明

  1. 通过在父类之后声明它们,使目标视图控制器符合UICollectionViewDelegateUICollectionViewDataSource协议:

     class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { @IBOutlet weak var collectionView: UICollectionView! //... } 
  2. viewDidLoad方法中注册关联的nib或单元的类,并将数据源和委托协议关联到视图控制器类:

      let cellIdentifier = "cellIdentifier" override func viewDidLoad() { super.viewDidLoad() //if you use xibs: self.collectionView.register(UINib(nibName:"MyCollectionCell", bundle: nil), forCellWithReuseIdentifier: cellIdentifier) //or if you use class: self.collectionView.register(MyCollectionCell.self, forCellWithReuseIdentifier: cellIdentifier) self.collectionView.delegate = self self.collectionView.dataSource = self } 
  3. 实现在UICollectionViewDelegateUICollectionViewDataSource协议中声明的方法:

      let objects = ["Cat", "Dog", "Fish"] func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.objects.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! MyCollectionCell //in this example I added a label named "title" into the MyCollectionCell class cell.title.text = self.objects[indexPath.item] return cell } 
  4. 在模拟器(或在真实的设备上)运行你的应用程序和..等voilà! 🙂

在这里输入图像说明

欲了解更多信息: https : //developer.apple.com/reference/uikit/uicollectionview

确定首先,您必须拥有您的集合视图的IBOutlet并实现像这样的方法

 class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{ @IBOutlet var collectionView: UICollectionView! override func viewDidLoad() { super.viewDidLoad() count = 9; let nib = UINib(nibName: "yourItemView", bundle: nil) collectionView.registerNib(nib, forCellWithReuseIdentifier: "yourItemView") self.collectionView.delegate = self self.collectionView.dataSource = self } 

确定在添加一个xib文件的函数中,接下来你必须创build从UICollectionViewCell扩展的,当你完成这个时你必须重写下一个方法

 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return count // the numbers of items } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {//size of your item for screen sizes let wsize = UIScreen.mainScreen().bounds.size.width switch(wsize){ case 414: return CGSize(width: 190, height: 102) case 375: return CGSize(width: 190, height: 102) case 320: return CGSize(width: 174, height: 102) default: return CGSize(width: 174, height: 102) } } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("yourItemView", forIndexPath: indexPath) as! yourItemView return cell } 

这是一切,祝你好运