iOS Swift 5 UICollectionView NIB 寄存器不工作,我使用 XIB 文件

iOS Swift 5 UICollectionView NIB register Not Working, i using XIB file

我的 UICollectionView:

@IBOutlet weak var mediaProfileCollectionView: UICollectionView!

我在 viewDidLoad() 中添加此代码:

mediaProfileCollectionView.register(UINib(nibName: "MediaProfileCollectionView", bundle: nil), forCellWithReuseIdentifier: "MediaProfileCollectionViewCell")

然后我用在:

    viewModelProfile.mediaProfileCollectionView
        .asObservable()
        .bind(to: mediaProfileCollectionView.rx.items(cellIdentifier: "MediaProfileCollectionViewCell", cellType: MediaProfileCollectionViewCell.self)) { (row,data,cell) in
            cell.setMediaImage(from: data)
        }.disposed(by: disposeBag)

然后我收到错误消息:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'MediaProfileCollectionView'

谁能帮帮我..

您很可能想为单元而不是视图本身注册 xib:

mediaProfileCollectionView.register(
   UINib(nibName: "MediaProfileCollectionViewCell", bundle: nil), 
   forCellWithReuseIdentifier: "MediaProfileCollectionViewCell"
)