我试图让一个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/) –