【发布时间】:2016-07-21 14:07:36
【问题描述】:
我已成功创建具有 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
在 parentCell.xib 中:
类设置为ParentCell,标识符设置为ParentCellIdentifier
在 ViewController 我有 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";
}
上面的设置就像一个魅力。 不,我想添加 ChildCell,使用与以前相同的 xib:
@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
【问题讨论】:
-
什么是崩溃请发布错误
-
添加编辑:它是发送到实例的无法识别的选择器。
标签: ios objective-c iphone uicollectionview