[英]UIViewController xib file containing a collectionview with a custom cell
我有一個xib文件,其中包含一個名為FullScreenGalleryVC
的UIViewController
。 它包含一個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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.