ios UICollectionView - 不显示单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21196271/
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-08-30 23:09:55  来源:igfitidea点击:

UICollectionView - Not displaying cells

iosobjective-cipaduicollectionview

提问by Ke Vin

What I'm trying to achieve is a 3x3 table with UICollectionView. But it just doesn't display any cells when running the application. It does display the background color of the CollectionViewthough (which I set to blue) Can someone help me here?

我想要实现的是一个带有UICollectionView. 但它在运行应用程序时不显示任何单元格。它确实显示了CollectionView虽然(我设置为蓝色)的背景颜色,有人可以在这里帮助我吗?

What I have done is create a storyboardand simply dragged a CollectionViewControlleronto it. This controller also brought a nested CollectionViewwith it. Then I simply used the settings to make its background blue and to add a cell to it of which I made the background purple (both only for testing). Finally dragged a UILabelonto it. Now when run it just displays the blue background but no cells even though they are clearly visible in the editor. I've then tried to create a custom UICollectionViewController, assign the class to the CollectionViewControllerinstead of the standard class and did the same to the cell (assign a custom UICollectionViewCell). In the custom UICollectionViewControllerI implemented the following methods:

我所做的是创建一个storyboard并简单地将一个CollectionViewController拖到它上面。这个控制器还带了一个嵌套的CollectionView。然后我简单地使用设置将其背景设为蓝色,并向其中添加一个单元格,我将其设为背景为紫色(均仅用于测试)。最后拖了一个UILabel就可以了。现在运行时它只显示蓝色背景,但没有单元格,即使它们在编辑器中清晰可见。然后,我尝试创建一个 custom UICollectionViewController,将类分配给 theCollectionViewController而不是标准类,并对单元格执行相同操作(分配一个 custom UICollectionViewCell)。在自定义中,UICollectionViewController我实现了以下方法:

-(NSIntegear)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 9;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    AboutYourGoalsMultiSelectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    [[cell goal]setText:@"T"];

    return cell;
}

I set the identifier of the cell to "Cell" and made an outlet connection of dataSource and delegate between the CollectionView and the custom UICollectionViewController. Everything compiles fine. So what am I missing? Shouldn't it have displayed something way before doing all these tweaks by simply using the editor?

我将单元格的标识符设置为“Cell”,并在 CollectionView 和自定义 UICollectionViewController 之间建立了数据源和委托的出口连接。一切都编译得很好。那么我错过了什么?在通过简单地使用编辑器进行所有这些调整之前,它不应该以某种方式显示吗?

The storyboard editor with all the changes I did

带有我所做的所有更改的故事板编辑器

采纳答案by Ke Vin

Since I didn't find a direct solution to it I've tried to set up a new project and did the same changes and tweaks and surprisingly, this time, everything worked from the beginning. I then copied the storyboard to my old project, deleted all the old files and replaced it with the files from the new project. Everything is working good so far. I have no idea what the actual error was but I'm happy it's working now. Thanks to everyone who had a look at my question!

由于我没有找到直接的解决方案,我尝试建立一个新项目并进行了相同的更改和调整,令人惊讶的是,这一次,一切从一开始就有效。然后我将故事板复制到我的旧项目中,删除所有旧文件并用新项目中的文件替换它。到目前为止,一切都运行良好。我不知道实际的错误是什么,但我很高兴它现在可以工作了。感谢所有看过我问题的人!

回答by Alex Stone

See my answer here, in short, you need to delete this lineif you are working with a storyboard:

在这里查看我的回答,简而言之,如果您正在使用故事板,则需要删除此行

 // Register cell classes
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

回答by Patricia Heimfarth

A common source of error is that dataSource and delegate are not set in the Outlets

一个常见的错误来源是在 Outlets 中没有设置 dataSource 和 delegate

回答by Rami Enbashi

You have an error in the first line of your code.

您的代码的第一行有错误。

-(NSIntegear)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

NSIntegearshould be NSInteger.

NSIntegear应该是NSInteger

Not sure if that's the problem though because xcode shouldn't allow you to run your project with this error. I just tried following the exact same steps you mentioned (without the label) but with the above correction and it worked fine for me. Here is a screenshot:

不确定这是否是问题所在,因为 xcode 不应该允许您在出现此错误的情况下运行您的项目。我只是尝试按照您提到的完全相同的步骤(不带标签)进行操作,但是经过上述更正,对我来说效果很好。这是一个屏幕截图:

enter image description here

在此处输入图片说明

回答by Mohammad Abraq

If you are trying to create just a simple CollectionViewController (not custom i.e. by implementation) and your cells are not displaying when application runs just follow the following steps:-

如果您只想创建一个简单的 CollectionViewController(不是自定义的,即通过实现)并且您的单元格在应用程序运行时没有显示,请按照以下步骤操作:-

  • First of all you have to create the cocoa touch class (if not created) for your UICollectionViewController. FILE -> NEW -> COCOA TOUCH CLASS then choose UICollectionViewController from drop down list of subclass.
  • Now under your identity inspector section of your CollectionViewController add your class by choosing from the drop down list of Custom Class.
  • Now open your created class and edit your methods like this:

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    //#warning Incomplete method implementation -- Return the number of sections
    return 1
     }
     override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    //#warning Incomplete method implementation -- Return the number of items in the section
    return 1
    }
    
  • Only edit "return" value (which is 0 by default) just simply write the number of cells/items you want to display or you have created (in this case here i only had created one Cell).

  • 首先,您必须为 UICollectionViewController 创建可可触摸类(如果未创建)。FILE -> NEW -> COCOA TOUCH CLASS 然后从子类的下拉列表中选择 UICollectionViewController。
  • 现在,在您的 CollectionViewController 的身份检查器部分下,通过从自定义类的下拉列表中进行选择来添加您的类。
  • 现在打开你创建的类并像这样编辑你的方法:

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    //#warning Incomplete method implementation -- Return the number of sections
    return 1
     }
     override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    //#warning Incomplete method implementation -- Return the number of items in the section
    return 1
    }
    
  • 仅编辑“返回”值(默认情况下为 0)只需简单地写下您要显示或已创建的单元格/项目的数量(在本例中,我只创建了一个单元格)。

[ios] [swift3] [xcode]

[ios] [swift3] [xcode]