Ios 子类化自定义UICollectionViewCell

Ios 子类化自定义UICollectionViewCell,ios,objective-c,iphone,uicollectionview,Ios,Objective C,Iphone,Uicollectionview,我已成功创建了具有3个标签的自定义UICollectionViewCell 现在我想对它进行子类化,添加dateToDisplay属性,并在setDateToDisplay中更改标签的字符串(使用自定义日期格式化程序) parentCell的创建和使用: ParentCellIdentifier.h static NSString *ParentCellIdentifier = @"ParentCellIdentifier"; @interface ParentCell : UICol

我已成功创建了具有3个标签的自定义UICollectionViewCell

现在我想对它进行子类化,添加dateToDisplay属性,并在setDateToDisplay中更改标签的字符串(使用自定义日期格式化程序)

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
-(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";
}
@interface ChildCell : ParentCell
@property (nonatomic, strong) NSDate* dateToDislpay;//labels of parentCell updated using dateformatters on setDateToDisplay:
@end
在parentCell.xib中:

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
-(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";
}
@interface ChildCell : ParentCell
@property (nonatomic, strong) NSDate* dateToDislpay;//labels of parentCell updated using dateformatters on setDateToDisplay:
@end
类设置为
ParentCell
,标识符设置为
ParentCellIdentifier

在ViewController中,我有
UICollectionView

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
-(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";
}
@interface ChildCell : ParentCell
@property (nonatomic, strong) NSDate* dateToDislpay;//labels of parentCell updated using dateformatters on setDateToDisplay:
@end
上面的设置非常有效。 否我想添加ChildCell,使用与前面相同的xib:

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
-(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";
}
@interface ChildCell : ParentCell
@property (nonatomic, strong) NSDate* dateToDislpay;//labels of parentCell updated using dateformatters on setDateToDisplay:
@end
-(void)setupCollectionView
保持不变,为ParentCellIdentifier注册ParentCell.xib

-(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
当试图设置子单元格的属性时,它崩溃。这是正确的,因为在ParentCell.xib中,单元格的类是ParentCell。 在更改了xib中的类之后,它就可以工作了,但我想继续使用我的xib中的父类,因为我将使用相同的xib,但只使用不同的格式化程序

我怎么做

EDIT1: 收到:

-[ParentCell setDateToDislpay:]: unrecognized selector sent to instance

您需要进入xib文件并将类类型更改为
ChildCell
,这将修复崩溃

要查看子类,请参见此处:


您必须重写
initWithFrame:
awakeFromNib
您需要进入xib文件并将类类型更改为
ChildCell
,这应该可以修复崩溃

要查看子类,请参见此处:


您必须重写
initWithFrame:
awakeFromNib

由于您有register
[UINib-nibWithNibName:@“ParentCell”bundle:nil]
父单元格,并且在项目的单元格中,您是
子单元格的创建对象,但它是父单元格而不是子单元格
并且父单元格没有
dateToDislpay
属性

您可以在print类中进行检查


您的继承方式不正确,您应该阅读一些教程或演示,因为您注册了
[UINib nibWithNibName:@“ParentCell”bundle:nil]
父单元格,并且您是
ChildCell
的创建对象,但它是父单元格而不是子单元格,所以崩溃了 并且父单元格没有
dateToDislpay
属性

您可以在print类中进行检查


你做继承的方式不对,你应该阅读一些教程或一些演示

什么是崩溃请发布错误添加编辑:它是无法识别的选择器发送到实例。什么是崩溃请发布错误添加编辑:它是无法识别的选择器发送到实例。这就是我所做的。但是,我想坚持使用xib中的ParentClass,因为我将使用父级的xib为标签创建具有不同格式的多个子类。但是,我想坚持使用xib中的ParentClass,因为我将使用父级的xib为标签创建具有不同格式化程序的多个子类。真正的问题是如何在不同的视图类中使用相同的xib。我在这里看不出为什么要使用父单元格,父单元格包含的IBOutlets是weakI知道的,我在问题中提到过。真正的问题是如何对不同的视图类使用相同的xib。我在这里看不出为什么使用父单元格,父单元格包含的IBOUTLES很弱