2013-01-22 29 views
46

Tôi đang cố gắng để tạo ra một lớp con UICollectionViewCell với liên kết một xib, tôi phải làm điều này: Tôi có tạo một file xib mới và tôi có thêm một UICollectionViewCell trong nó, sau đó tôi có tạo lớp con này file:Tạo UICollectionViewCell lớp con với xib

@interface MyCell : UICollectionViewCell 

@property (weak, nonatomic) IBOutlet UILabel *label; 
@end 

Ngoài ra tôi đã liên kết trong lớp tập tin chủ sở hữu tùy chỉnh lớp MyCell trong xây dựng giao diện, và tôi đã thêm một UILabel, sau đó trong UICollectionView viewDidLoad của tôi, tôi làm điều này:

[self.collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"MyCell"]; 

UINib *cellNib = [UINib nibWithNibName:@"MyCell" bundle:nil]; 
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"MyCell"]; 

Cũng như trong này:

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
MyCell *cell = (MyCell*)[cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath]; 


cell.label.text = @"Cell Text"; 


return cell; 
} 

Tuy nhiên điều này không làm việc, tôi nhận được lỗi này:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x907eca0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key label.' 

Tôi đã làm gì sai? Làm thế nào tôi có thể kết nối một lớp con UICollectionViewCell với xib và hiển thị nó trong một số UICollectionView?

EDIT:

tôi có làm điều này:

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 

NSString *identifier = @"MyCell"; 

static BOOL nibMyCellloaded = NO; 

if(!nibMyCellloaded) 
{ 
    UINib *nib = [UINib nibWithNibName:@"MyCell" bundle: nil]; 
    [cv registerNib:nib forCellWithReuseIdentifier:identifier]; 
    nibMyCellloaded = YES; 
} 

MyCell *cell = (MyCell*)[cv dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath]; 


cell.labelCell.text = @"Text"; 


return cell; 
} 
+0

vì vậy trong trường hợp sử dụng xib với lớp được kết nối, có vẻ như bạn phải gọi collectionXem đăng ký trên cả tệp nib và lớp bạn đã liên kết với xib? là luồng công việc của bạn gọi trình khởi tạo initWithFrame của lớp ô? – topwik

+0

"Mã đã chỉnh sửa" của bạn bị lỗi. Các biến tĩnh bên trong một hàm thành viên được chia sẻ bởi tất cả các cá thể. Nếu bạn có nhiều trường hợp bộ điều khiển xem, chỉ người đầu tiên sẽ gọi 'registerNib'. –

Trả lời

48

Hãy chắc chắn rằng các tế bào vào file .xib biết loại tế bào là những gì.

Chọn ô trên người xây dựng giao diện của bạn

enter image description here

và sau đó trên thanh tra danh tính

enter image description here

Sau đó kết hợp các nhãn của bạn với tài sản của bạn. (Tôi nghĩ bạn đã làm điều đó)

Sau đó, tôi khuyên bạn nên để xác minh nếu bạn đã nạp file .xib trên phương pháp cellForItemAtIndexPath: bạn

NSString *identifier = @"MyCell";  

    static BOOL nibMyCellloaded = NO; 

    if(!nibMyCellloaded) 
    { 
     UINib *nib = [UINib nibWithNibName:@"MyCell" bundle: nil]; 
     [cv registerNib:nib forCellWithReuseIdentifier:identifier]; 
     nibMyCellloaded = YES; 
    } 
+0

Tôi đã chỉnh sửa câu hỏi của mình ... hãy xem ... – Piero

+0

không, tôi xin lỗi, tôi đã phóng to ô và biết công việc ... – Piero

+4

bạn không thể gọi registerNib một lần trên viewDidLoad thay vì làm kiểm tra mỗi khi xem bộ sưu tập đang yêu cầu một ô? – topwik

20

Bạn có thể tìm câu trả lời trong tài liệu này UICollectionView adding UICollectionCell.

Only UICollectionView inside StoryBoard have UICollectionViewCell inside. If use XIB, create a new XIB with CellName.xib, add CollectionViewCell to it, specify name of UICollectionView custom class. After that use registerNib.Sample code: https://github.com/lequysang/TestCollectionViewWithXIB

+0

Cảm ơn. mã của bạn là mã duy nhất làm việc cho tôi. Tôi chỉ tự hỏi tại sao sử dụng làm cho thuộc tính 'UILabel' của ô 'yếu'? Tôi nghĩ tất cả IBOutlets phải là 'strong'. – Ali

+1

@Ali liên quan đến câu hỏi của bạn: http://stackoverflow.com/a/7729141/351810 – XCool