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

查看:147
本文介绍了在带有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:

设计单元格

创建您的主视图控制器,其中包括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. 使目标视图控制器符合 UICollectionViewDelegate UICollectionViewDataSource 协议,方法是在父类之后声明它们:

  1. Make the target view controller conform to the UICollectionViewDelegate and UICollectionViewDataSource protocols by declaring them after the father class:

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
     }
    


  • 执行在 UICollectionViewDelegate UICollectionViewDataSource 协议:

     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天全站免登陆