将 UICollectionViewController 添加到 UIViewController 不起作用 [英] Adding UICollectionViewController to UIViewController Not Working

查看:30
本文介绍了将 UICollectionViewController 添加到 UIViewController 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIViewController 和一个 UICollectionViewController.我使用以下内容将 UICollectionViewController 添加到 UIViewController 中:

UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizo​​ntal];self.toolsCollectionViewController = [[ToolsCollectionViewController alloc] initWithCollectionViewLayout:flowLayout];self.toolsCollectionViewController.view.backgroundColor = [UIColor clearColor];[self addChildViewController:self.toolsCollectionViewController];[self.view addSubview:self.toolsCollectionViewController.view];[self.toolsCollectionViewController didMoveToParentViewController:self];

问题是,当我添加集合时,出现纯黑屏幕,没有显示任何单元格

<前>#import "ToolsCollectionViewController.h"@interface ToolsCollectionViewController()@结尾@implementation ToolsCollectionViewController静态 NSString * const 重用标识符 = @"Cell";- (void)viewDidLoad {[超级viewDidLoad];//取消注释以下行以保留演示文稿之间的选择//self.clearsSelectionOnViewWillAppear = NO;//注册单元类[self.collectionView registerClass:[ToolsCollectionViewCell 类] forCellWithReuseIdentifier:reuseIdentifier];//加载视图后进行任何其他设置.}- (void)didReceiveMemoryWarning {[超级 didReceiveMemoryWarning];//处理任何可以重新创建的资源.}/*#pragma mark - 导航//在基于故事板的应用程序中,您通常需要在导航前做一些准备工作- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {//使用 [segue destinationViewController] 获取新的视图控制器.//将选中的对象传递给新的视图控制器.}*/#pragma 标记- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {#warning 不完整的实现,返回节数返回 1;}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {#warning 执行不完整,返回项数返回 10;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {ToolsCollectionViewCell *cell = (ToolsCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];NSLog(@"Cell");//配置单元格返回单元格;}#pragma 标记/*//取消注释此方法以指定在跟踪期间是否应突出显示指定的项目- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {返回是;}*//*//取消注释此方法以指定是否应选择指定的项目- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {返回是;}*//*//取消注释这些方法以指定是否应为指定项显示操作菜单,并对在该项上执行的操作做出反应- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {返回否;}- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {返回否;}- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {}*/@结尾

我已经为单元格的循环创建了一个 NSLog 并且这似乎工作正常.不知道我错过了什么.

解决方案

可能有 3 种不同的方案.

  1. 您实施的配色方案导致了问题
  2. 未正确调用委托和数据源.
  3. 您的单元格不存在或重用标识符无效.

I have a UIViewController and a UICollectionViewController. I add the UICollectionViewController to the UIViewController using the following:

UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];

self.toolsCollectionViewController = [[ToolsCollectionViewController alloc] initWithCollectionViewLayout:flowLayout];
self.toolsCollectionViewController.view.backgroundColor = [UIColor clearColor];

[self addChildViewController:self.toolsCollectionViewController];
[self.view addSubview:self.toolsCollectionViewController.view];
[self.toolsCollectionViewController didMoveToParentViewController:self];

The problem is that when I add the collection I get a solid black screen appear with no cells on display

#import "ToolsCollectionViewController.h"

@interface ToolsCollectionViewController ()

@end

@implementation ToolsCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;

    // Register cell classes
    [self.collectionView registerClass:[ToolsCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark 

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete implementation, return the number of sections
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of items
    return 10;
}

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

    ToolsCollectionViewCell *cell = (ToolsCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    NSLog(@"Cell");
    // Configure the cell

    return cell;
}

#pragma mark 

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {

}
*/

@end

I have created an NSLog for the loop through the cells and that seems to be working fine. Not sure what I am missing.

解决方案

There could be 3 different scenarios.

  1. Color schemes you have implemented is causing the issue
  2. Delegate and Datasource have not been called properly.
  3. either your cell not exist or reuse identifier is not valid.

这篇关于将 UICollectionViewController 添加到 UIViewController 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆