繁体   English   中英

在UIView中使用CollectionView和xib文件

[英]Using CollectionView in UIView with xib file

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

我尝试使用这种方式,但它比UICollectionView需要大量的时间和管理

在此输入图像描述

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

  1. 首先,创建一个继承自UICollectionViewCell的新类。 选择是否要包含xib以轻松设计单元格: 在此输入图像描述

  2. 使用Interface Builder或以编程方式设计单元。

  3. 创建主视图控制器,包括带有集合视图的xib(或故事板),并通过Interface Builder将其链接到关联的类。 或者,您可以以编程方式将集合视图添加到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文件的函数中,接下来必须从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
    }

这就是全部,祝你好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM