Ich habe erfolgreich benutzerdefinierte UICollectionViewCell erstellt, die 3 Beschriftungen hat.Subclassing benutzerdefinierte UICollectionViewCell
Jetzt möchte ich es unterklassifizieren, dateToDisplay -Eigenschaft hinzufügen und in setDateToDisplay Zeichenfolgen ändern (mit benutzerdefinierten Datumsformatierern).
Erstellung und Nutzung von 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:
Klasse auf ParentCell
und Identifier auf ParentCellIdentifier
In Viewcontroller Ich habe 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";
}
Das Setup oben arbeitete wie ein Charme. Nein Ich mag ChildCell, mit gleichen xib wie zuvor hinzuzufügen:
@interface ChildCell : ParentCell
@property (nonatomic, strong) NSDate* dateToDislpay;//labels of parentCell updated using dateformatters on setDateToDisplay:
@end
-(void)setupCollectionView
links das gleiche, die Registrierung ParentCell.xib für 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
Beim Versuch zu setzen Eigenschaft Kind Zelle, es stürzt ab. Es ist korrekt, weil in der ParentCell.xib-Klasse der Zelle ParentCell ist. Nach dem Ändern der Klasse in xib funktioniert es, aber ich möchte mit meiner Elternklasse in xib bleiben, da ich die gleiche XIB verwenden werde, aber nur mit verschiedenen Formatierern.
Wie kann ich es tun?
EDIT1: Absturz erhalten:
-[ParentCell setDateToDislpay:]: unrecognized selector sent to instance
lesen, was ist Absturz bitte Fehler –
Hinzugefügt bearbeiten schreiben: es Unbekannter Selektor gesendet Instanz . – izik461