Create UICollectionViewCell subclass with xib
Solution 1
Make sure the cell on the .xib file know what's the type of the cell.
Select the cell on your interface builder
and then on the identity inspector
Subsequently associate your labels with your properties. (I think you already did that)
Then I'd recommend to verify if you already loaded the .xib file on your cellForItemAtIndexPath:
method
NSString *identifier = @"MyCell";
static BOOL nibMyCellloaded = NO;
if(!nibMyCellloaded)
{
UINib *nib = [UINib nibWithNibName:@"MyCell" bundle: nil];
[cv registerNib:nib forCellWithReuseIdentifier:identifier];
nibMyCellloaded = YES;
}
Solution 2
You can find the answer in this document UICollectionView adding UICollectionCell.
Only UICollectionView inside StoryBoard have UICollectionViewCell inside. If use XIB, create a new XIB with CellName.xib, add CollectionViewCell to it, specify name of UICollectionView custom class. After that use registerNib.Sample code: https://github.com/lequysang/TestCollectionViewWithXIB
Related videos on Youtube
Piero
Updated on July 09, 2022Comments
-
Piero almost 2 years
I'm trying to create a
UICollectionViewCell
subclass with linked a xib, I have do this: I have create a new xib file and I have add aUICollectionViewCell
in it, then I have create this subclass file:@interface MyCell : UICollectionViewCell @property (weak, nonatomic) IBOutlet UILabel *label; @end
Also I have linked in the file owner custom class the
MyCell
class in interface builder, and I have added aUILabel
, then in myUICollectionView
viewDidLoad I do this:[self.collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"MyCell"]; UINib *cellNib = [UINib nibWithNibName:@"MyCell" bundle:nil]; [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"MyCell"];
As well as in this:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { MyCell *cell = (MyCell*)[cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath]; cell.label.text = @"Cell Text"; return cell; }
However this doesn't work, I receive this error:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x907eca0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key label.'
What did I do wrong? How can I connect a
UICollectionViewCell
subclass to a xib, and display it in aUICollectionView
?EDIT:
i have do this:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { NSString *identifier = @"MyCell"; static BOOL nibMyCellloaded = NO; if(!nibMyCellloaded) { UINib *nib = [UINib nibWithNibName:@"MyCell" bundle: nil]; [cv registerNib:nib forCellWithReuseIdentifier:identifier]; nibMyCellloaded = YES; } MyCell *cell = (MyCell*)[cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath]; cell.labelCell.text = @"Text"; return cell; }
-
topwik about 11 yearsso in the case of using a xib with a connected class, it seems you have to call collectionView register on both the nib file and the class you linked with the xib? is your workflow invoking the cell class' initWithFrame initializer?
-
Hai Feng Kao over 10 yearsYour "edited code" is buggy. Static variables inside a member function are shared by all instances. If you have multiple view controller instances, only the first one will call
registerNib
.
-
-
Piero over 11 yearsi have edited my question...take a look please...
-
Piero over 11 yearsno i'm sorry, i have enlarge the cell and know works...
-
topwik about 11 yearscan't you just call the registerNib once on viewDidLoad instead of doing the check every time the collection view is asking for a cell?
-
Ali almost 11 yearsThanks. your code is the only one that worked for me. I'm just wondering why use make the
UILabel
property of cellweak
? I thought all IBOutlets must bestrong
. -
XCool over 10 years@Ali regarding your question: stackoverflow.com/a/7729141/351810
-
Hai Feng Kao about 9 yearsUsing
static
is a bad practice. The static variable is shared for all instances. The second view controller of the same class will not load the .xib file becausenibMyCellloaded
will be set toYES
by the first one. -
Eugene Tiutiunnyk almost 9 yearsImportant note here is that .XIB File's Owner should NOT be set to MyCell, but only the root element Class. Otherwise the solution doesn't work.