I've succesfully created custom UICollectionViewCell that has 3 labels.
Now I want to subclass it, add dateToDisplay property and in setDateToDisplay change labels's strings (with custom date formatters).
Creation and usage of parentCell:
ParentCellIdentifier.h
static NSString *ParentCellIdentifier = @"ParentCellIdentifier";
@interface ParentCell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UILabel *firstLabel;
@property (strong, nonatomic) IBOutlet UILabel *secondLabel;
@property (strong, nonatomic) IBOutlet UILabel *thirdLabel;
-(void)setupDefaults;
@end
In parentCell.xib:
Class set to ParentCell
and Identifier set to ParentCellIdentifier
In ViewController I have UICollectionView
:
-(void)setupCollectionView {
[_daysCollectionView registerNib:[UINib nibWithNibName:@"ParentCell" bundle:nil] forCellWithReuseIdentifier:ParentCellIdentifier];
_daysCollectionView.delegate = self;
_daysCollectionView.dataSource = self;
_daysCollectionView.backgroundColor = [UIColor clearColor];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
ParentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ParentCellIdentifier forIndexPath:indexPath];
cell.firstLabel.text = @"first";
cell.secondLabel.text = @"second";
}
The setup above worked like a charm. No I want to add ChildCell, using same xib as previously:
@interface ChildCell : ParentCell
@property (nonatomic, strong) NSDate* dateToDislpay;//labels of parentCell updated using dateformatters on setDateToDisplay:
@end
-(void)setupCollectionView
left the same, registering ParentCell.xib for ParentCellIdentifier
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
ChildCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ParentCellIdentifier forIndexPath:indexPath];
NSDate* date = [_dates objectAtIndex:indexPath.row];
cell.dateToDislpay = date; //here goes the error
When trying to set property of child cell, it crashes. It's correct, because in ParentCell.xib class of the cell is ParentCell. After changing the class in xib, it works, but I want to stick with my parent class in xib, since I will use the same xib, but with different formatters only.
How can I do it?
EDIT1: crash received:
-[ParentCell setDateToDislpay:]: unrecognized selector sent to instance