Reputation: 1721
I have a UICollectionView that use a custom cell:
[_collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cellIdentifier"];
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.articolo = [_array objectAtIndex:indexPath.row];
I want to pass the object articolo to my cell, so I create a property:
@property (strong, nonatomic) NSMutableDictionary *articolo;
but if I try to read the property in one of this method:
@implementation CollectionViewCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {NSLog(@"Articolo: %@",_articolo);}
return self;
}
- (id)initWithCoder:(NSCoder*)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self) {NSLog(@"Articolo: %@",_articolo);}
return self;
}
- (void) awakeFromNib
{
[super awakeFromNib];
NSLog(@"Articolo: %@",_articolo);
}
I get always a null value, where is the error? how can I pass this object to my cell?
Upvotes: 0
Views: 215
Reputation: 2251
Try this, it may help u.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.articolo =[[NSMutableDictionary alloc] initWithDictionary:[_array objectAtIndex:indexPath.row]];
return cell;
}
Upvotes: 1
Reputation: 3395
Your code is perfect, you will always get null value in any of the above method because it is called when your cell is initialized.
You can get value by putting setter method like this.
-(void)setArticolo:(NSMutableDictionary *)articolo {
_articolo = articolo;
}
Upvotes: 1