在带有 xib 文件的 UIView 中使用 CollectionView [英] Using CollectionView in UIView with xib file

查看:19
本文介绍了在带有 xib 文件的 UIView 中使用 CollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i'm doing with this, i want to use CollectionView, but i haven't seen prototype cell, and don't know how to use CollectionView in this case, can someone help me ?

我尝试这样使用,但它比 UICollectionView 花费大量时间并且难以管理

I try to use like this way but it take alot of time and hard to manage than UICollectionView

推荐答案

使用 UICollectionView 的主要方式是以编程方式管理逻辑.

The main way to use UICollectionView is by managing the logic programmatically.

  1. 首先,创建一个继承自 UICollectionViewCell 的新类.选择是否要包含 xib 以轻松设计单元格:

  1. First, create a new class which inherits from UICollectionViewCell. Choose if you want to include a xib to easily design your cell:

使用 Interface Builder 或以编程方式设计您的单元.

Design your cell with Interface Builder or programmatically.

创建您的主视图控制器,包括一个带有 集合视图 的 xib(或故事板),并通过 Interface Builder 将其链接到关联的类.或者,您可以以编程方式将集合视图添加到您的 UIViewController

Create your main view controller including a xib (or a storyboard) with the collection view inside and link it to the associated class via Interface Builder. Alternatively you can add a collection view programmatically to your UIViewController

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

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

    @IBOutlet weak var collectionView: UICollectionView!

    //...
} 

  • viewDidLoad 方法中为您的单元注册关联的笔尖或类,并将数据源和委托协议关联到视图控制器类:

  • Register the associated nib or the class for your cell in the viewDidLoad method and associate the datasource and delegate protocols to the view controller class:

     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
     }
    

  • 实现在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
     }
    

  • 在模拟器(或真实设备)中运行您的应用程序,然后……等等!:)

  • Run your app in the simulator (or on a real device) and.. Et voilà! :)

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

    这篇关于在带有 xib 文件的 UIView 中使用 CollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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