在 Xcode 中使用 CollectionView 时遇到问题 - 显示黑屏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17262848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:31:59  来源:igfitidea点击:

Having trouble with CollectionView in Xcode - display's black screen

iphoneobjective-cxcodeipadcollectionview

提问by asympthom-e

Basically I've been writing iOS apps for about 3 days (after reading a book to get the basic knowledge of design patterns and frameworks etc) and 1 entire day has been spent on this. I cannot get my CollectionViewto display. I've got a storyboard that starts with a TabBarViewand the two tabs (one is a TableViewand the other is just a UIView) work, but the third tab (UICollectionView) just displays a black screen even after setting up. How I set it up:

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

1) dragged a ViewController to the storyboard

1) 拖一个 ViewController 到故事板

2) made a relationship seguebetween my UITabBarControllerto the new ViewController

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

3) dragged a UICollectionViewto the new ViewController

3)拖一个UICollectionView到新的ViewController

4) dragged 4 UICollectionViewCell'sto the CollectionView

4)拖动4UICollectionViewCell'sCollectionView

5) dragged 4 UILabelsinto said CollectionViewCell's

5) 将 4UILabels拖入表示CollectionViewCell's

6) made a connection between the new CollectionView'sdelegate & data source and the header file of my CollectionViewclass (this might be where I went wrong, I did not make the connection to my ViewController.hclass as I used this one for my UITableViewand thought a new class was necessary)

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

7) declared the following methods in my CollectionView.mfile

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

I get the feeling there is something I missed out, as this logic makes sense to me as a programmer but, having such little experience in Xcode, maybe I forgot to link up this class to my CollectionViewor something. Does anybody know where I went wrong?

我觉得我错过了一些东西,因为这个逻辑对我作为程序员来说很有意义,但是,在 Xcode 中的经验很少,也许我忘记将这个类与我的CollectionView或其他东西联系起来。有人知道我哪里出错了吗?

BTW in case you're wondering, I'm using this CollectionView more of a navigation technique than to display wonderful images or anything, I am going to populate them with generic images later when they actually show up in the emulator. thanks

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

回答by mavericks

It is not displaying anything because default color of label is black, you'll have to change the color of text to any light color to see text over cell.

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

Now, 

->Add a new file which should be subclass of UICollectionViewCell, Ctrl+drag from label to this class.(for eg I took mainViewCell)and label property name I took labelText

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

->Declare reuseIdentifier for cell from Attribute Inspector,(mainCell, I took)

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

->Import cellClass to your UIViewControllerClass,

-> 将 cellClass 导入您的UIViewController类,

->Using this method to display any text

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

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

hope this would help...

希望这会有所帮助...

回答by JRo

Did you tell your view controller(the one that contains the collection view) to use UICollectionViewDelegateFlowLayout? For example,

你告诉你的视图控制器(包含集合视图的那个)使用 UICollectionViewDelegateFlowLayout 吗?例如,

@interface RootViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>

Also, have you set the reuse identifier for your cell? In Interface Builder set this in the attributes inspector. Then go to the cellForItemAtIndexPath: method and change this line to use your identifier. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];

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

Check out UICollectionViewDelegateFlowLayout in the documentation for methods used to size cells and set margins and spacing.

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

回答by Hackless

The step 6) might be inadequate. Have you ctrl-dragged the collectionView to the controller icon beneath the main view directly? You would have been prompted in a black dialog box for connecting dataSource and delegate. The connections are made in XCode this way. Alternatively you could explicitly set the dataSource as self in the controller implementation.

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

回答by thatzprem

Remove registerClass code from the default view controller code snippet

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

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