【问题标题】:Having trouble with CollectionView in Xcode - display's black screenXcode 中的 CollectionView 出现问题 - 显示黑屏
【发布时间】:2013-06-23 16:30:38
【问题描述】:

基本上我已经写了大约 3 天的 iOS 应用程序(在阅读了一本书以了解设计模式和框架等的基本知识之后),并且已经花了一整天的时间。我无法显示我的CollectionView。我有一个以TabBarView 开头的故事板,两个选项卡(一个是TableView,另一个只是UIView)工作,但第三个选项卡(UICollectionView)只显示黑屏即使在设置之后。 我是如何设置的:

1) 将 ViewController 拖到情节提要

2) 在我的UITabBarController 和新的ViewController 之间建立了关系segue

3) 将UICollectionView 拖到新的ViewController

4) 将4个UICollectionViewCell's拖到CollectionView

5) 将 4 个UILabels 拖入上述CollectionViewCell's

6) 在新的CollectionView's 委托和数据源与我的CollectionView 类的头文件之间建立了连接(这可能是我出错的地方,我没有将我的ViewController.h 类连接为我将这个用于我的UITableView,并认为有必要开设一个新课程)

7) 在我的CollectionView.m 文件中声明了以下方法

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//Populates each cell using the data given (no data in this case).
//Should return a CollectionViewCell.
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    //Gets the number of cells in each section.
    //Should return an integer value.
    return 2;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    //Tells the collection view the number of sections it should have.
    //Should return an integer value.
    return 4;
}

我觉得我错过了一些东西,因为这个逻辑对我作为一名程序员来说是有意义的,但是,在 Xcode 方面的经验如此之少,也许我忘记将这个类链接到我的 CollectionView 或其他东西。有人知道我哪里出错了吗?

顺便说一句,如果您想知道,我使用此 CollectionView 更多的是一种导航技术,而不是显示精彩的图像或任何东西,稍后当它们实际显示在模拟器中时,我将使用通用图像填充它们。 谢谢

【问题讨论】:

    标签: iphone objective-c xcode ipad collectionview


    【解决方案1】:

    它不显示任何内容,因为标签的默认颜色是黑色,您必须将文本颜色更改为任何浅色才能看到单元格上的文本。

    Now, 
    

    ->添加一个新文件,该文件应该是UICollectionViewCell的子类,Ctrl+从标签拖动到这个类。(例如我取了ma​​inViewCell)和标签属性名称我拿了 labelText

    ->从属性检查器声明单元的重用标识符,(mainCell,我拿了)

    ->将 cellClass 导入到您的 UIViewController 类中,

    ->使用此方法显示任意文本

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        mainViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"mainCell" forIndexPath:indexPath];
        cell.labelText.text = @"What-ever-you-want-display";
        return cell;
    }
    

    希望这会有所帮助...

    【讨论】:

      【解决方案2】:

      您是否告诉您的视图控制器(包含集合视图的控制器)使用 UICollectionViewDelegateFlowLayout?例如,

      @interface RootViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
      

      另外,您是否为您的单元设置了重用标识符?在 Interface Builder 中,在属性检查器中设置此项。然后转到 cellForItemAtIndexPath: 方法并更改此行以使用您的标识符。 UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];

      查看文档中的 UICollectionViewDelegateFlowLayout 以了解用于调整单元格大小以及设置边距和间距的方法。

      【讨论】:

        【解决方案3】:

        步骤 6) 可能不够。您是否直接将 collectionView 拖到主视图下方的控制器图标上?将在一个黑色对话框中提示您连接数据源和委托。通过这种方式在 XCode 中建立连接。 或者,您可以在控制器实现中将 dataSource 显式设置为 self。

        【讨论】:

          【解决方案4】:

          从默认视图控制器代码sn-p中移除registerClass代码

          [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-06-01
            • 2020-07-31
            • 1970-01-01
            相关资源
            最近更新 更多