Iphone Xcode-display'中的CollectionView出现问题;黑屏

Iphone Xcode-display'中的CollectionView出现问题;黑屏,iphone,objective-c,xcode,ipad,collectionview,Iphone,Objective C,Xcode,Ipad,Collectionview,基本上,我已经写了大约3天的iOS应用程序(在读了一本书以获得设计模式和框架等的基本知识之后),花了整整一天的时间在这上面。我无法显示我的CollectionView。我有一个故事板,以一个TabBarView和两个选项卡(一个是TableView,另一个只是UIView)开始工作,但第三个选项卡(UICollectionView)即使在设置之后也只显示一个黑屏。 我如何设置它: 1) 将ViewController拖动到情节提要 2) 在myUITabBarController与新的ViewC

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

1) 将ViewController拖动到情节提要

2) 在my
UITabBarController
与新的
ViewController
之间建立了一个关系
segue

3) 将
UICollectionView
拖动到新的
ViewController

4) 将4
UICollectionViewCell的
拖动到
CollectionView

5) 将4个
UILabels
拖到所述
CollectionViewCell的

6) 在新的
CollectionView的
delegate和数据源与我的
CollectionView
类的头文件之间建立了连接(这可能是我出错的地方,我没有连接到我的
ViewController.h
类,因为我将此类用于我的
UITableView
,并且认为需要一个新类)

7) 在my
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更多的是一种导航技术,而不是显示精彩的图像或任何东西,我将在以后它们实际出现在模拟器中时用通用图像填充它们。
谢谢

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

@interface RootViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
@接口RootViewController()
另外,您是否为单元设置了重用标识符?在界面生成器中,在属性检查器中设置此选项。然后转到cellForItemAtIndexPath:方法并更改此行以使用您的标识符。 UICollectionViewCell*单元格=[collectionView dequeueReusableCellWithReuseIdentifier:@“MyCell”forIndexPath:indexPath]

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

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

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

Now, 
->添加一个新文件,该文件应为UICollectionViewCell的子类,按住Ctrl键并将其从label拖动到此类。(例如,我使用了mainViewCell)和label属性名称

->从属性检查器为单元格声明reuseIdentifier(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;
}

希望这将有助于…

从默认视图控制器代码段中删除registerClass代码

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