[iOS]通过xib定义Cell然后关联UICollectionView

先新建一个View的xib,然后删掉自动生成的View,拖进一个UICollectionCell,再新建一个对应的UIView继承UICollectionCell类。

OK,接下来该连outlet的就连。

设置delegate和dataSource

然后通常我们都要这样,

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("PersonalizedSettingsMovieCell", forIndexPath: indexPath) as PersonalizedSettingsMovieCell
        
        return cell
    }

但仅仅是这样是不行的,找不到 PersonalizedSettingsMovieCell 的 Identifier,所以我们要

1.在xib里面设置Cell的Identifier。

2.UICollectionCell要设置

collectionView.registerNib(UINib(nibName: "PersonalizedSettingsMovieCell", bundle: nil), forCellWithReuseIdentifier: "PersonalizedSettingsMovieCell")

  

原文地址:https://www.cnblogs.com/helloandroid/p/4287671.html