我試圖讓一個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,但沒有顯示任何單元格。
[你遵循Appcoda教程嗎?](http://www.appcoda.com/ios-collection-view-tutorial/) –