Ответ 1
Метод, который вам нужно реализовать, - (instancetype)initWithFrame:(CGRect)rect
Я добавил коллекцию в viewDidLoad, как это...
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 10, 10) collectionViewLayout:flowLayout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.collectionView registerClass:[CameraCell class] forCellWithReuseIdentifier:CameraCellReuseIdentifier];
[self.collectionView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.collectionView];
И у меня есть подкласс UICollectionViewCell, называемый CameraCell с init, как это...
- (id)init
{
self = [super init];
if (self) {
// Add customisation here...
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imageChanged:) name:@"image" object:nil];
self.contentView.backgroundColor = [UIColor yellowColor];
self.imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.clipsToBounds = YES;
[self.imageView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView addSubview:self.imageView];
NSDictionary *views = NSDictionaryOfVariableBindings(_imageView);
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_imageView]|"
options:0
metrics:nil
views:views]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_imageView]|"
options:0
metrics:nil
views:views]];
}
return self;
}
Но когда я запускаю приложение, есть вид коллекции, и я могу прокручивать его, но я не вижу никаких ячеек. Я добавил контрольную точку в ячейке init и никогда ее не вызывает. Есть ли другой метод, который я должен переопределить?
ИЗМЕНИТЬ
Когда я регистрирую ячейку в cellForItemAtIndexPath...
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CameraCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CameraCellReuseIdentifier forIndexPath:indexPath];
NSLog(@"%@", cell);
return cell;
}
Отображает правильный класс...
<CameraCell: 0x1f07ba30; baseClass = UICollectionViewCell; frame = (0 20; 320 280); layer = <CALayer: 0x1f07bb40>>
Метод, который вам нужно реализовать, - (instancetype)initWithFrame:(CGRect)rect
Недавно я попробовал это в SDK iOS7, и мне пришлось переопределить initWithCoder
, потому что initWithFrame
никогда не вызывался.
Если ячейка загружена из StoryBoard, вам нужно переопределить инициализаторы следующим образом:
Swift
override init?(coder aDecoder: NSCoder) {
super.init(coder:aDecoder)
//You Code here
}
Objective-C
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
//You code here
}
Если вы загружаете из наконечника, вам нужно переопределить инициализаторы следующим образом:
Swift
override init(frame: CGRect) {
super.init(frame:frame)
//You Code here
}
Objective-C
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
//You code here
}
В моем случае, когда я использую [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 10, 10) collectionViewLayout:flowLayout]
для программной инстанции коллекцииView, initWithFrame
никогда не вызывается, а initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
сделал.