ios UICollectionViewCells 没有出现在 UICollectionView 中

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

UICollectionViewCells not appearing in a UICollectionView

iosuicollectionviewuicollectionviewcell

提问by virindh

As the title suggests I have a UICollectionViewthat is hooked up to a UIViewControllerthrough IB. I have a UICollectionViewsubclass called imageCell. In the viewcontroller viewDidLoadmethod, I register the class like this:

正如标题所暗示的,我有一个UICollectionView连接到UIViewControllerIB 的。我有一个UICollectionView名为imageCell. 在 viewcontrollerviewDidLoad方法中,我像这样注册类:

CGRect collectionViewRect = CGRectMake(384, 60, 728, 924);
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
imageCollectionView = [[UICollectionView alloc] initWithFrame:collectionViewRect collectionViewLayout:flow];
[imageCollectionView registerClass:[imageCell class] forCellWithReuseIdentifier:@"Cell"];
imageCollectionView.delegate = self;
imageCollectionView.dataSource = self;

I then call a method called getPhotosin viewDidAppear(for UI reasons) that gets photos from a service and then I call [imagCcollectionView reloadData];inside getPhotos.

然后调用一个名为方法getPhotosviewDidAppear(用于UI原因),从一个服务获取照片,然后我打电话[imagCcollectionView reloadData];里面getPhotos

Then in cellForItemAtIndexPathI do the following:

然后在cellForItemAtIndexPath我执行以下操作:

imageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
GRKAlbum *album = [albums objectAtIndex:indexPath.item];
cell.titleLabel.text = album.name;
return cell;

I know I am not doing anything wrong here because the method is being called and cellis not niland neither is album.

我知道我在这里没有做错任何事情,因为正在调用该方法,并且cell不是nil,也不是album

So, when I run all of this code, the UICollectionViewappears, but nothing is in it. All I can see is the background color of the collectionView. Any help at all would be greatly appreciated.

所以,当我运行所有这些代码时,会UICollectionView出现,但里面什么也没有。我只能看到collectionView. 任何帮助都将不胜感激。

Note: I did NOT create an XIB for the imageCellclass when I made it and I am using Storyboards.

注意:我在制作时没有为imageCell班级创建 XIB,而且我正在使用故事板。

回答by Anil Varghese

There are some ways to debug this situation.

有一些方法可以调试这种情况。

  1. Register UICollectionViewCellclass instead of your customCelland set the cell's background color in cellForItem...

  2. If the cells are appearing in step one try to use your own cell class and set its background

  1. 注册UICollectionViewCell类而不是您的customCell并将单元格的背景颜色设置为cellForItem...

  2. 如果单元格出现在第一步中,请尝试使用您自己的单元格类并设置其背景

Are you using storyboard? then..

你在使用故事板吗?然后..

You dont have to register any cell for the collection view. You can use the prototype cell, just give the proper id for the cell.You can design everything in your prototype cell. Custom class may not be needed in all the cases.

您不必为集合视图注册任何单元格。您可以使用原型单元格,只需为单元格提供正确的 id。您可以在原型单元格中设计所有内容。并非在所有情况下都需要自定义类。

回答by rdelmar

If you created the cell in the storyboard, then just remove the register class statement. You don't need to register anything (for a collection view or table view) if you make the cell in a storyboard (in fact, it screws things up if you do so). The only thing you need to do is make sure that you set the identifier in IB.

如果您在故事板中创建了单元格,则只需删除 register 类语句。如果您在故事板中创建单元格,则不需要注册任何内容(对于集合视图或表视图)(实际上,如果您这样做,事情就会搞砸)。您唯一需要做的就是确保在 IB 中设置标识符。

回答by BhushanVU

I had the same issue fixed with replacing conventional registercellline with below line

registercell用下面的线替换传统线解决了同样的问题

[self.collection registerNib:[UINib nibWithNibName:@"nibname" bundle:nil] forCellWithReuseIdentifier:identifier];

Swift 4.2 version: (thanks Nikolay Suvandzhiev)

Swift 4.2 版本:(感谢 Nikolay Suvandzhiev)

self.collection.register(UINib(nibName: "nibname", bundle: nil), forCellWithReuseIdentifier: identifier)

回答by nenchev

I felt like a dummy when I realized my collection view cells were being covered up by my navigation bar. Sooo, make sure your navigation bar (if you have one) isn't covering the collection view cells you're testing with.

当我意识到我的集合视图单元格被导航栏覆盖时,我感觉自己像个傻瓜。Sooo,确保您的导航栏(如果有的话)没有覆盖您正在测试的集合视图单元格。

回答by Yao Li

Sometimes the collection view cell is black (same color with background, invisible, hidden is no), reloading the visible cells is a work around.

有时集合视图单元格是黑色的(与背景相同的颜色,不可见,隐藏是没有),重新加载可见单元格是一种解决方法。

回答by jrturton

You're instantiating a new collection view in your code, but aren't adding the view to the screen. You say you're already using storyboards, my guess is that what you can see is the collection view from your storyboard, which presumably isn't connected to a datasource or delegate? Either that or you're not calling reload on the right collection view.

您正在代码中实例化一个新的集合视图,但没有将该视图添加到屏幕上。你说你已经在使用故事板了,我猜你能看到的是故事板中的集合视图,它大概没有连接到数据源或委托?或者你没有在正确的集合视图上调用 reload 。

Either configure your collection view in the storyboard (and set up an outlet to it for anything that must be done in code) or set it up completely in code. At the moment I think you have two collection views.

在故事板中配置您的集合视图(并为必须在代码中完成的任何事情设置一个出口)或完全在代码中设置它。目前我认为您有两个集合视图。

回答by alvin

I made it using Xcode 6.2 storyboard. Hope it helps

我使用 Xcode 6.2 故事板制作了它。希望能帮助到你

#import <UIKit/UIKit.h>

@interface InfoSecurity : UIViewController
@property (strong, nonatomic) IBOutlet UICollectionView *tabsCV;

@end

.m
**************

#import "InfoSecurity.h"
#import "TabsCell.h"

@interface InfoSecurity ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@end

@implementation InfoSecurity

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self.tabsCV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"TabsCell"];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 03;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 01;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(90, 50);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *reuseIdentifier = @"tabscellID";


    TabsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    cell.TabTitleLabel.text= @"OPTIONS";

    return cell;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}