首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UICollectionView registerClass: forCellWithReuseIdentifier方法破坏UICollectionView

UICollectionView registerClass: forCellWithReuseIdentifier方法破坏UICollectionView
EN

Stack Overflow用户
提问于 2012-10-12 18:41:09
回答 1查看 28.9K关注 0票数 30

registerClass:forCellWithReuseIdentifier:方法的作用是什么?根据苹果的开发者文档,这应该是

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

当我试图使用它,我的项目,我得到一个黑色的集合视图。当我删除它时,一切都很好。

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

发布于 2013-01-20 10:06:12

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

如果单元格类是用代码编写的,则使用registerClass: UICollectionView方法执行注册,否则使用registerNib。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12865196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档