由于 Storyboard 的限制,我正在以编程方式创建 UICollectionView。这一切正常,当我想添加一个UICollectionViewCell
我执行以下操作:
[collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"ID"];
我想知道如何使用“Cell”类中的自定义 init 方法,因为我无法执行以下操作:
[collectionView registerClass:[[Cell class]init_custom]forCellWithReuseIdentifier:@"ID"];
问题:如何使用自定义 init 方法UICollectionViewCell
class?
如果我理解正确,那么我会创建集合视图单元格的子类。
首先用你想要的一切设置你的单元。
@interface MyCollectionViewCell : UICollectionViewCell
// Your custom cell
@end
@implementation MyCollectionViewCell
// Your custom cell
@end
然后为每个集合视图创建一个仅覆盖 init 的子类。
@interface MyCollectionViewCellForCollectionView1 : MyCollectionViewCell
@end
@implementation MyCollectionViewCellForCollectionView1
- (instancetype)init // Only override -init
{
self = [super init];
if (self) {
// Setup for collection view one
}
return self;
}
@end
@interface MyCollectionViewCellForCollectionView2 : MyCollectionViewCell
@end
@implementation MyCollectionViewCellForCollectionView2
- (instancetype)init // Only override -init
{
self = [super init];
if (self) {
// Setup for collection view two
}
return self;
}
@end
然后,对于每个不同的集合视图,您注册一个子类。
[collectionView1 registerClass:[MyCollectionViewCellForCollectionView1 class] forCellWithReuseIdentifier:@"ID"];
[collectionView2 registerClass:[MyCollectionViewCellForCollectionView2 class] forCellWithReuseIdentifier:@"ID"];
这将为您提供所需的单独的自定义 init 方法,但请确保将所有功能保留在基类中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)