【问题标题】:Adding UICollectionViewController to UIViewController Not Working将 UICollectionViewController 添加到 UIViewController 不起作用
【发布时间】:2016-02-26 18:37:54
【问题描述】:

我有一个UIViewController 和一个UICollectionViewController。我使用以下代码将UICollectionViewController 添加到UIViewController

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

问题是,当我添加集合时,我得到一个纯黑屏幕,没有显示单元格

#import "ToolsCollectionViewController.h" @interface ToolsCollectionViewController () @结尾 @实现工具CollectionViewController 静态 NSString * 常量重用标识符 = @"Cell"; - (void)viewDidLoad { [超级视图DidLoad]; // 取消注释以下行以保留演示文稿之间的选择 // self.clearsSelectionOnViewWillAppear = NO; // 注册单元格类 [self.collectionView registerClass:[ToolsCollectionViewCell 类] forCellWithReuseIdentifier:reuseIdentifier]; // 加载视图后做任何额外的设置。 } - (void)didReceiveMemoryWarning { [super 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(@"细胞"); // 配置单元格 返回单元格; } #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,这似乎工作正常。不知道我错过了什么。

【问题讨论】:

  • 看起来你的实现是正确的。尝试在 cellForItemAtIndexPath 方法中为单元格添加一些背景颜色。
  • 设置子视图控制器的框架

标签: ios objective-c uiview uicollectionview


【解决方案1】:

您是否正确设置了 collectionView 的委托和数据源?如果没有这两个,collectionView 将显示为空,并且只会显示一个使用背景颜色着色的矩形。

您很可能需要将以下内容放在上面的- (void)viewDidLoad 中: self.collectionView.delegate = self; self.collectionView.dataSource = self;

【讨论】:

    【解决方案2】:

    可能有 3 种不同的情况。

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

    【讨论】:

      猜你喜欢
      • 2014-08-18
      • 1970-01-01
      • 1970-01-01
      • 2017-09-10
      • 1970-01-01
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多