7

Tôi đang cố gắng hiển thị UICollectionView bên trong trình điều khiển chế độ xem được trình bày có phương thức. Ứng dụng cho iPad iOS 7.Không thể hiển thị UICollectionView để hiển thị các ô

tôi đã tạo ra lớp con của UIViewController (với một ngòi) và thêm nó như thế này:

MyViewController *controller = [[MyViewController alloc] init]; 
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller]; 
navController.modalPresentationStyle = UIModalPresentationFullScreen; 
navController.navigationBar.barStyle = UIBarStyleBlackTranslucent; 

[self presentViewController:navController animated:YES completion:nil]; 

điều khiển Quan điểm này là trở thành đại biểu và nguồn dữ liệu cho UICollectionView của tôi vì vậy tôi đã thêm UICollectionViewDataSource và UICollectionViewDelegate vào tiêu đề.

Tôi đã đặt một UICollectionView vào ngòi bút và thêm một lối thoát để MyViewController:

@property (strong, nonatomic) IBOutlet MyCollectionView *collectionViewController; 

Tôi đã thêm này trong viewDidLoad trong MyViewController:

self.collectionViewController.dataSource = self; 
self.collectionViewController.delegate = self; 

Tôi cũng đã thêm theo sau MyViewController:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    NSLog(@"Items in section: %d", itemsArray.count); // returns correct amount 

    return itemsArray.count; 
} 


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"cellForItemAtIndexPath %@", indexPath); // returns as expected 

    static NSString *identifier = @"MyCell"; 

    [self.collectionViewController registerClass:[MyCollectionCell class] forCellWithReuseIdentifier:identifier]; 

    MyCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    UIImageView *myImageView = (UIImageView *)[cell viewWithTag:100]; 
    myImageView.image = [UIImage imageNamed:[itemsArray objectAtIndex:indexPath.row]]; 

    return cell; 
} 

Tôi cũng đã thiết lập một lớp con của UICollection ViewCell với số nhận dạng được đặt thành MyCell và được thêm vào UIImageView với thẻ 100.

Bất cứ khi nào tôi hiển thị bộ điều khiển chế độ xem này, tôi sẽ nhận được thanh điều hướng như mong đợi, nhưng chế độ xem UICollection tôi đã thêm vào nib là hư không để được nhìn thấy. Tất cả những gì tôi thấy là màu đen, nơi chế độ xem bộ sưu tập nên có. Nếu tôi thay đổi màu nền của MyCollectionView từ mặc định thành trắng, tôi sẽ thấy màu trắng, nơi chế độ xem bộ sưu tập phải là. Dường như nó sẽ hiển thị MyCollectionView, nhưng không hiển thị bất kỳ ô nào.

+0

[Bạn đã làm theo hướng dẫn của Appcoda?] (Http://www.appcoda.com/ios-collection-view-tutorial/) –

Trả lời

20

Nếu bạn liên kết bộ sưu tập của mìnhXem và dữ liệu riêng của mìnhSource và ủy quyền trong tệp xib của bạn, bạn không cần phải thiết lập mã này trên mã của mình.

Tiếp theo, bạn cần phải đăng ký UICollectionViewCell của bạn:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Register Nib 
    [self.collectionView registerNib:[UINib nibWithNibName:CollectionViewCell_XIB bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:CollectionViewCell_ID]; 
} 

CollectionViewCell_XIB là tên của xib di động của bạn CollectionViewCell_ID là ID của tế bào

của bạn Và bạn cần phải thực hiện cellForItemAtIndexPath như thế này:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CollectionViewCell *cell = (CollectionViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCell_ID forIndexPath:indexPath]; 

    // Configure cell with data 
    UIImageView *myImageView = (UIImageView *)[cell viewWithTag:100]; 
    myImageView.image = [UIImage imageNamed:[itemsArray objectAtIndex:indexPath.row]]; 

    // Return the cell 
    return cell; 
} 
+1

Hoàn toàn tuyệt vời, thưa bạn. Cảm ơn bạn! – beev

+3

bạn được chào đón;) không ngần ngại thêm +1: p –

24

Một điểm quan tâm khác là nếu bạn đặt số nhận dạng của ô trong nib hoặc bảng phân cảnh, không đăng ký ngòi/lớp trong bộ điều khiển xem bộ sưu tập. Làm cái này hay cái kia, nhưng không phải cả hai.

+4

Tuyệt vời, tôi đã gặp phải vấn đề tương tự và cách này giải quyết được vấn đề đó. –

+3

Đó là vấn đề đối với tôi. Thx –

+0

NÀY !!!! Tôi không thể tin được – SteBra

Các vấn đề liên quan