2013-05-13 25 views
22

Tôi đã thêm một cái nhìn bộ sưu tập trong viewDidLoad như thế này ...UICollectionViewCell lớp con init không bao giờ chạy

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

Và tôi có một lớp con UICollectionViewCell gọi CameraCell với init như thế này ...

- (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; 
} 

Nhưng khi tôi chạy ứng dụng, chế độ xem bộ sưu tập ở đó và tôi có thể cuộn nó nhưng tôi không thể thấy bất kỳ ô nào. Tôi đã thêm một breakpoint trong init của ô và nó không bao giờ được gọi. Có phương pháp nào khác mà tôi phải ghi đè không?

EDIT

Khi tôi đăng nhập các tế bào trong cellForItemAtIndexPath ...

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CameraCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CameraCellReuseIdentifier forIndexPath:indexPath]; 

    NSLog(@"%@", cell); 

    return cell; 
} 

Nó hiển thị các lớp đúng ...

<CameraCell: 0x1f07ba30; baseClass = UICollectionViewCell; frame = (0 20; 320 280); layer = <CALayer: 0x1f07bb40>> 
+1

Ý tôi là, bạn đang gọi initWithFrame thay vì init ... – rocky

Trả lời

40

Phương pháp bạn cần phải thực hiện là - (instancetype)initWithFrame:(CGRect)rect

+1

OMG Tôi không thể tin rằng tôi đã làm điều đó. Cảm ơn. – Fogmeister

+1

Nhưng khung không được điều chỉnh bởi chính 'collectionView' — biểu hiện' initWithFrame' này từ 'collectionViewCell' vô ích? – Honey

+3

Đối số khung bạn nhận được là vô dụng, nhưng bạn vẫn sẽ cần phải khởi tạo các thuộc tính của riêng mình tại đây. – Aleph7

27

Gần đây tôi đã thử tính năng này trong SDK iOS7 và tôi phải ghi đè initWithCoder vì không bao giờ được gọi là initWithFrame.

+1

Bạn có tải nó từ bảng phân cảnh hoặc ngòi? Hoặc tất cả trong mã? – Fogmeister

+0

Ô được tải từ Bảng phân cảnh. –

+31

^Vậy đó là lý do. 'initWithCoder' là bộ khởi tạo được chỉ định cho các khung nhìn được xây dựng từ bảng phân cảnh. 'initWithFrame' là cho các khung nhìn được xây dựng theo chương trình. –

-1

Trong trường hợp của tôi, khi tôi đang sử dụng [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 10, 10) collectionViewLayout:flowLayout] để lập trình nhanh một bộ sưu tậpXem, initWithFrame không bao giờ được gọi, nhưng initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout đã làm.

9

Nếu các tế bào được nạp từ một kịch bản sau đó bạn sẽ muốn ghi đè initializers như thế này:

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 
} 

Nếu bạn đang tải từ một ngòi, sau đó bạn sẽ muốn ghi đè lên initializers như thế này:

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 
} 
Các vấn đề liên quan