1

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
izik461
  • 1,131
  • 12
  • 32

2 Answers2

0

You need to go into the xib file and change the class type to ChildCell and that should fix your crash.

To subclass see here: https://stackoverflow.com/a/5056886/848719

You'll have to override initWithFrame: and awakeFromNib.

Community
  • 1
  • 1
kurryt
  • 210
  • 4
  • 10
  • That's what I did. I want, however, to stick with the ParentClass in xib since I am going to create multiple child classes with different formatters for labels using the the parent's xib. – izik461 Jul 21 '16 at 14:44
0

You got crash because you have register [UINib nibWithNibName:@"ParentCell" bundle:nil] Parent Cell and in cell for item you are create object of ChildCell , but it is parent cell not child cell and parent cell does not have dateToDislpay property .

you can check same with print class

you are doing inheritance wrong way, you should read some tutorial or read some demos

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
  • 1
    I know that and I mentioned it in my question. The real question is how to use same xib for different classes of views. – izik461 Jul 22 '16 at 07:45
  • there is no reason i can see here that why you are taking parent cell, parent cell contains the IBOutlets which are weak – Prashant Tukadiya Jul 22 '16 at 07:51