2013-05-13 7 views
22

は、私はこのようなのviewDidLoadでコレクションビューを追加したUICollectionViewCellサブクラスのinit ... ...

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]; 

を実行していないと私はこのようなinitでCameraCell呼ばUICollectionViewCellサブクラスを持っているん

- (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にブレークポイントを追加しましたが、呼び出されることはありません。オーバーライドする必要がある別の方法がありますか?

EDIT

私は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>> 
+1

私が意味する、あなたは私がそれをした – rocky

答えて

40

あなたが実装する必要がある方法があります- (instancetype)initWithFrame:(CGRect)rect

+1

OMG私は信じることができない...代わりのinitのinitWithFrameを呼んでいます。ありがとう。 – Fogmeister

+1

フレームは 'collectionView'自身によって管理されているのではなく、この' initWithFrame'を 'collectionViewCell'から無用にしていますか? – Honey

+3

取得するフレーム引数は無用ですが、ここで独自のプロパティを初期化する必要があります。 – Aleph7

27

最近、iOS7 SDKでこれを試しましたが、initWithFrameが呼び出されなかったので、initWithCoderを上書きしなければなりませんでした。

+1

ストーリーボードやペン先からロードしていましたか?コード内のすべてですか? – Fogmeister

+0

セルはストーリーボードからロードされます。 –

+31

^それで、それが理由です。 'initWithCoder'は、ストーリーボードから構築されたビューの初期化子です。 'initWithFrame'はプログラム的に構築されたビューです。 –

-1

私の場合、[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 10, 10) collectionViewLayout:flowLayout]を使用して、コレクションビューをプログラムでインスタンス化すると、initWithFrameは呼び出されませんが、initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layoutは実行されません。

9

細胞はストーリーボードからロードされている場合、あなたはこのような初期化子を上書きしたいと思うでしょうが:

スウィフト

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 
} 

の場合ペン先からロードしている場合は、次のようにイニシャライザをオーバーライドする必要があります。

スウィフト

override init(frame: CGRect) { 
    super.init(frame:frame) 
    //You Code here 
} 

のObjective-Cは

- (id)initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    //You code here 
} 
関連する問題