簡體   English   中英

UIViewController xib文件包含具有自定義單元格的collectionview

[英]UIViewController xib file containing a collectionview with a custom cell

我有一個xib文件,其中包含一個名為FullScreenGalleryVCUIViewController 它包含一個UICollectionView ,該UICollectionView包含一個自定義UICollectionViewCell ,其標識符為fullscreen_cell

當我想創建一個新的FullScreenGalleryVC ,我調用FullscreenGalleryVC.display(from: self)

這是func:

class func display(from sourcevc:UIViewController){
    let vc=UINib(nibName: "FullscreenGalleryVC", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! FullscreenGalleryVC
    sourcevc.present(vc, animated: true, completion: nil)
}

我收到此錯誤:

無法使同類視圖出隊:標識符為fullscreen_cell的UICollectionElementKindCell-必須為該標識符注冊一個筆尖或一個類,或在情節提要中連接原型單元


如果我將自定義單元格放在另一個xib文件中並調用register(nibClass, forCellWithReuseIdentifier: cellId)

如果我不將此FullScreenGalleryVC類放在單獨的xib文件中(並將其保存在我的主故事板上),它將很好地工作。


但是我在應用程序和操作擴展中都使用了此類,所以這就是為什么我想使用公用文件而不是復制所有內容的原因。 有沒有辦法做到這一點,還是我必須將自定義單元放在另一個xib文件中才能正常工作?

在此處輸入圖片說明

如果您是FullScreenGalleryVC控制器的xib文件,則應使用register(nibClass, forCellWithReuseIdentifier: cellId)告訴集合視圖如何創建給定類型的新單元格。

let cellNib = UINib(nibName: "FullScreenGalleryCell", bundle: .main) 
collectionView.register(cellNib, forCellWithReuseIdentifier: "fullscreen_cell")

如果使用storyboard cellId ,則具有指定cellId的收藏cellId視圖已知道該storyboard cellId

我認為,可以將storyboard用於FullScreenGalleryVC並將其用於其他類。

從特定的storyboard viewController,

let storyboard = UIStoryboard(name: "YourStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "storyboardId")
self.present(controller, animated: true, completion: nil)

圖片1 圖片2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM