Fog*_*ter 27 ios uicollectionview uicollectionviewcell
我在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];
Run Code Online (Sandbox Code Playgroud)
我有一个名为CameraCell的UICollectionViewCell子类,其中包含一个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;
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行应用程序时,集合视图就在那里,我可以滚动它,但我看不到任何单元格.我在单元格的init中添加了一个断点,它永远不会被调用.还有其他方法我必须覆盖吗?
编辑
当我在cellForItemAtIndexPath中记录单元格时......
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CameraCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CameraCellReuseIdentifier forIndexPath:indexPath];
NSLog(@"%@", cell);
return cell;
}
Run Code Online (Sandbox Code Playgroud)
它显示正确的类...
<CameraCell: 0x1f07ba30; baseClass = UICollectionViewCell; frame = (0 20; 320 280); layer = <CALayer: 0x1f07bb40>>
Run Code Online (Sandbox Code Playgroud)
Ale*_*dro 44
您需要实现的方法是 - (instancetype)initWithFrame:(CGRect)rect
Arc*_*eja 27
我最近在iOS7 SDK中试过这个,我不得不覆盖,initWithCoder因为initWithFrame从未调用过.
Bar*_*ker 16
如果从StoryBoard加载单元格,那么您将希望覆盖这样的初始化程序:
迅速
override init?(coder aDecoder: NSCoder) {
super.init(coder:aDecoder)
//You Code here
}
Run Code Online (Sandbox Code Playgroud)
Objective-C的
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
//You code here
}
Run Code Online (Sandbox Code Playgroud)
如果你从一个笔尖加载,那么你会想要覆盖这样的初始化器:
迅速
override init(frame: CGRect) {
super.init(frame:frame)
//You Code here
}
Run Code Online (Sandbox Code Playgroud)
Objective-C的
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
//You code here
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29245 次 |
| 最近记录: |