UICollectionView Using Swift

后端 未结 4 2156
生来不讨喜
生来不讨喜 2020-12-18 09:01

In My Project I created Cell in UICollectionViewCell

Its got Error Terminating app due to uncaught exception

The code as follow.

GalleryCell.

相关标签:
4条回答
  • 2020-12-18 09:32

    I added the following two lines in NextViewController.swift under viewDidLoad():

    var nibName = UINib(nibName: "GalleryCell", bundle:nil)
    
    collectionView.registerNib(nibName, forCellWithReuseIdentifier: "CELL")
    

    The problem was that I was not registering the nib. Now that I'm doing that, it's working fine.

    0 讨论(0)
  • 2020-12-18 09:33

    You need to register a cell class.

    In your viewDidLoad write.

    collectionView.registerClass(NSClassFromString("GalleryCell"),forCellWithReuseIdentifier:"CELL");
    

    Prior to calling the dequeueReusableCellWithReuseIdentifier:forIndexPath: method of the collection view, you must use this method or the registerNib:forCellWithReuseIdentifier: method to tell the collection view how to create a new cell of the given type. If a cell of the specified type is not currently in a reuse queue, the collection view uses the provided information to create a new cell object automatically.

    If you previously registered a class or nib file with the same reuse identifier, the class you specify in the cellClass parameter replaces the old entry. You may specify nil for cellClass if you want to unregister the class from the specified reuse identifier.

    Reference registerClass:forCellWithReuseIdentifier:

    0 讨论(0)
  • 2020-12-18 09:36

    I wanted to add another answer that helped me, in case it becomes useful to someone else.

    It seems that everyone else has solved this problem through the use of registerNib:forCellWithReuseIdentifier: or registerClass:forCellWithReuseIdentifier:. For me, there was a different resolution: in my storyboard, I selected the header and looked over in the Identity Inspector in the top section where it has fields for my Custom Class. Specifically, the Module field was blank. I needed to pull down the arrow next to that field and select the right target.

    Once I did this & saved the storyboard, the above crash no longer happened and I could see my custom header.

    0 讨论(0)
  • 2020-12-18 09:42

    the same for header/footer in swift:

        // register header accessory view:
        self.collectionView.registerClass(UICollectionReusableView.self,
            forSupplementaryViewOfKind: UICollectionElementKindSectionHeader,
            withReuseIdentifier: headerReuseIdentifier);
    
    0 讨论(0)
提交回复
热议问题