UICollectionView registerClass: forCellWithReuseIdentifier 方法会破坏 UICollectionView [英] UICollectionView registerClass: forCellWithReuseIdentifier method breaks UICollectionView

查看:10
本文介绍了UICollectionView registerClass: forCellWithReuseIdentifier 方法会破坏 UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

注册一个类以用于创建新的集合视图单元格."

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

推荐答案

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

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

  • numberOfItemsInSection
  • numberOfSectionsInCollectionView(不是必需的方法 - 参考这条评论)
  • cellForItemAtIndexPath

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

Then the registerClass / registerCell method 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 registerClass method.

此外,如果您正在寻找关于 registerCell 的作用以及如何使用它的详细说明,请查看 此链接并滚动到标题为单元格和视图重用"的底部部分.

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

这篇关于UICollectionView registerClass: forCellWithReuseIdentifier 方法会破坏 UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆