Xcode中的Collection View出现问题 - 显示黑屏

问题描述 投票:1回答:4

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

1)将ViewController拖到故事板上

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

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
4个回答
3
投票

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

Now, 

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

- >从Attribute Inspector为单元格声明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;
}

希望这会有所帮助......


0
投票

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

@interface RootViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>

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

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


0
投票

步骤6)可能不充分。您是否已将collectionView直接拖动到主视图下方的控制器图标?系统会在黑色对话框中提示您连接dataSource和delegate。连接以这种方式在XCode中进行。或者,您可以在控制器实现中将dataSource显式设置为self。


0
投票

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

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.