UICollectionView registerClass:forCellWithReuseIdentifier方法中断UICollectionView [英] UICollectionView registerClass: forCellWithReuseIdentifier method breaks UICollectionView

查看:663
本文介绍了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;
}


推荐答案

如果你已经创建了你在故事板中的UICollectionView,连接你的 dataSource 委托,你已经添加了所有必需的方法:

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

  • numberOfItemsInSection
  • numberOfSectionsInCollectionView (not a required method - refer to this comment)
  • cellForItemAtIndexPath

然后不需要 registerClass / registerCell 方法。但是,如果您需要重用视图,数据或单元格,则应使用这些方法,以便iOS可以根据需要填充UICollectionView。这也可以通过设置原型单元(与 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天全站免登陆