简体   繁体   English

iOS-缺少将自定义UICollectionViewCell添加到UICollectionView的功能

[英]iOS — missing something adding a custom UICollectionViewCell to a UICollectionView

Im trying to define my own custom UICollectionViewCell implementation and use it in a UICollectionView . 我试图定义我自己的自定义UICollectionViewCell实现并在UICollectionView使用它。 Here is what I have done. 这是我所做的。

1- I am using storyboards. 1-我正在使用情节提要。

2- I added a UICollectionView to another view, that was a scene on the storyboard. 2-我将UICollectionView添加到另一个视图,这是故事板上的一个场景。

3- I pointed datasource and delegate of the collection to the controller for the scene. 3-我将datasource和集合的delegate指向了场景的控制器。

4- I provided implementations of the required methods in the controller. 4-我在控制器中提供了所需方法的实现。

5- I created a cell view .h and .m file. 5-我创建了一个单元格视图.h.m文件。 In the .h file I do .h文件中

@interface HscCellView : UICollectionViewCell

6- I created a new view, so I got a new canvas, and in the appropriate section on the right I specified my class name (same as in step 5), and provided a unique identifier. 6-我创建了一个新视图,所以得到了一个新画布,并且在右侧的适当部分中指定了我的类名(与步骤5中相同),并提供了唯一的标识符。
7- In the controller for the scene, I defined an outlet in the .h file to the collection view I had dragged in. 7-在场景控制器中,我在.h文件中为我拖入的集合视图定义了一个出口。

@property (nonatomic, strong) IBOutlet UICollectionView *holesCollectionView;

and in viewDidLoad I do 然后在viewDidLoad

[self.holesCollectionView registerClass:[HscCellView class] forCellWithReuseIdentifier:@"cellView"];

note that cellView is the unique identifier I specified for my custom cell xib. 请注意, cellView是我为自定义单元xib指定的唯一标识符。

8- Finally, I implemented 8-最后,我实现了

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HscCellView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellView" forIndexPath:indexPath];

    return cell;

}

When I fire up the app, I get a bunch of cell views (9, since I specify 9 on the numberOfItemsInSection message) but they are not my custom cell view. 启动应用程序时,会得到一堆单元格视图(9,因为我在numberOfItemsInSection消息上指定了9),但它们不是我的自定义单元格视图。 They don't have the size, color, or label. 它们没有大小,颜色或标签。 I'm not seeing any errors. 我没有看到任何错误。 I'm stuck. 我被卡住了。

A few ancillary questions. 一些辅助问题。

  • When do I use register the nib and when do I register the class name? 什么时候使用注册笔尖,什么时候注册类名? I've seen it done both ways, but it seems that you pretty much always end up with both a nib and a class definition code, is that true?. 我已经看到它同时完成了两种工作,但是看来您几乎总是以笔尖和类定义代码结尾,是吗?

  • In step 6, when I specify the unique id in the canvas for the custom cell view, does that mean I don't need to register the class in viewDidLoad ? 在第6步中,当我在画布中为自定义单元格视图指定唯一ID时,这是否意味着我不需要在viewDidLoad注册该类?

Got it, I think. 我知道了。 I'll try to explain it and hopefully will be corrected if I'm wrong. 我会尽力解释,如果我做错了,希望会得到纠正。 I think this will be helpful for others. 我认为这对其他人会有帮助。 Based on the comments from my question, I determined that instances of my class were in fact being loaded. 根据我的问题的评论,我确定实际上正在加载我的类的实例。 However, the xib was being ignored. 但是,XIB被忽略了。 This implied that while my class was loaded, the xib was not being used. 这意味着在加载我的课程时,并未使用xib。 So, in step 7 in the question, I do 因此,在问题的第7步中

UINib *nib = [UINib nibWithNibName:@"HscCellView" bundle:nil];
    [self.holesCollectionView registerNib:nib forCellWithReuseIdentifier:@"cellView"];

Instead of the registering the class, and now things appear to work. 而不是注册课程,现在一切正常。

I am kind of curious as to why, since I linked the xib with my custom class, this is necessary, but I am no longer stuck. 我很好奇为什么,因为我将xib与我的自定义类链接了,所以这是必要的,但是我不再陷入困境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM