7

我試圖讓一個UICollectionView在模態呈現的視圖控制器內顯示。該應用程序是iPad等iOS 7無法獲取UICollectionView來顯示單元格

我創建了子類的UIViewController(用筆尖),並加入像這樣:

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

此視圖控制器是爲我UICollectionView委託和數據源所以我已經將UICollectionViewDataSource和UICollectionViewDelegate添加到標題中。

我已經把UICollectionView到筆尖,並增加了出口MyViewController:

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

我在MyViewController加入這個在viewDidLoad中:

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

我還添加了以下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; 
} 

我還設置了UICollection的子類ViewCell的標識符設置爲MyCell,並添加到它的標籤100的UIImageView。

每當我調出這個視圖控制器,我得到了導航欄的預期,但我已經添加到我的筆尖的UICollection視圖無處可見。我所看到的全是黑色,應該是收集視圖。如果我將MyCollectionView的背景顏色從默認值更改爲白色,我會在集合視圖應該顯示的地方看到白色。它似乎是提出了MyCollectionView,但沒有顯示任何單元格。

+0

[你遵循Appcoda教程嗎?](http://www.appcoda.com/ios-collection-view-tutorial/) –

回答

20

如果你在你的xib文件中鏈接你的collectionView和它自己的dataSource和委託,你不需要在你的代碼中設置它。

接下來,你需要註冊UICollectionViewCell:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

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

CollectionViewCell_XIB是你的廈門國際銀行的名稱 CollectionViewCell_ID是你的

的ID和你需要實現cellForItemAtIndexPath這樣的:

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

完全真棒,先生。謝謝! – beev

+3

不客氣;)毫不猶豫地加+1:p –

24

另一個值得關注的問題是,如果您設置了單元的標識符在筆尖或故事板中,不要在集合視圖控制器中註冊筆尖/類。做一個或另一個,但不是兩個。

+4

令人驚訝的是,我遇到了同樣的問題,這解決了它。 –

+3

這是我的問題。 Thx –

+0

這個!!!! omg我不敢相信 – SteBra

相關問題