ios UICollectionView registerClass: forCellWithReuseIdentifier 方法破坏了 UICollectionView

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

UICollectionView registerClass: forCellWithReuseIdentifier method breaks UICollectionView

iosobjective-cuicollectionviewuicollectionviewcell

提问by Janusz Chudzynski

What's the role of registerClass:forCellWithReuseIdentifier:method? According to Apple's developer documentation it's supposed to

registerClass:forCellWithReuseIdentifier:方法的作用是什么?根据 Apple 的开发人员文档,它应该

"Register a class for use in creating new collection view cells."

“注册一个用于创建新集合视图单元格的类。”

When I try to use it my project I get a black collection view. When I delete it everything works fine.

当我尝试在我的项目中使用它时,我得到了一个黑色的集合视图。当我删除它时一切正常。

#define cellId @"cellId"
#import "ViewController.h"
#import "Cell.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property(strong, nonatomic)NSMutableArray * photoArray;


@end

@implementation ViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"%@",_photoArray);
    _photoArray = [[NSMutableArray alloc]initWithCapacity:0];
    [_collectionView registerClass:[Cell class] forCellWithReuseIdentifier:cellId];
    for(int i=1;i<=12;i++)
    {
        NSString * imgName = [NSString stringWithFormat:@"%d.png",i];
        UIImage *img  = [UIImage imageNamed:imgName];
        [_photoArray addObject:img];
    }
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _photoArray.count;
}

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    Cell* cell = [_collectionView  dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    cell.cellImage.image = [_photoArray objectAtIndex:indexPath.row];
    return cell;
}

回答by Sam Spencer

If you've already created your UICollectionView in Storyboard, connected your dataSourceand delegate, and you have added all of the required methods:

如果您已经在 Storyboard 中创建了 UICollectionView,连接了您的dataSourcedelegate,并且您已经添加了所有必需的方法:

  • numberOfItemsInSection
  • numberOfSectionsInCollectionView(not a required method - refer to this comment)
  • cellForItemAtIndexPath
  • numberOfItemsInSection
  • numberOfSectionsInCollectionView(不是必需的方法 -请参阅此评论
  • cellForItemAtIndexPath

Then the registerClass/ registerCellmethod isn't required. However, if you need to reuse a view, data, or cells then you should use those methods so that iOS can populate your UICollectionView as needed. This can also be done in your Storyboard by setting the Prototype Cell(the same principle as the registerClassmethod.

那么registerClass/registerCell方法不是必需的。但是,如果您需要重用视图、数据或单元格,那么您应该使用这些方法,以便 iOS 可以根据需要填充您的 UICollectionView。这也可以在您的 Storyboard 中通过设置Prototype Cell来完成(与registerClass方法相同的原理。

Also, if you're looking for a good explanation on what registerCelldoes and how to use it check out this linkand scroll to the bottom section titled "Cell and View Reuse."

此外,如果您正在寻找关于registerCell它的作用和如何使用它的一个很好的解释,请查看此链接并滚动到标题为“单元格和视图重用”的底部部分。

回答by Steve

Agree with RazorSharp's answerand wanted to point out that that key phrase for me in the Techtopia linkis:

同意RazorSharp 的回答,并想指出Techtopia 链接中对我来说的关键短语是:

If the cell class was written in code, the registration is performed using the registerClass: method of UICollectionView, otherwise use registerNib

如果cell类是用代码编写的,则使用UICollectionView的registerClass:方法进行注册,否则使用registerNib