ios - UICollectionView 单元格未添加为 UICollectionView 的 subview

标签 ios objective-c xcode uicollectionview uicollectionviewcell

我是 UICollectionView 的新手。

我已关注this tutorial学习如何使用 UICollectionView,但问题是在 .xib (不使用 Storyboard)中我无法添加 UICollectionViewCell作为 UICollectionView 的 subview 。

在本教程中,单元格是在 .xib 中的 UICollectionView 下自动创建的。

如有任何帮助,我们将不胜感激。

最佳答案

该教程适用于在 xib 中的 stroy board 中工作,您必须首先创建自定义单元格创建 View Controller 添加 UicollectionView 来查看 Controller

enter image description here enter image description here

然后为集合单元格创建自定义类

enter image description here

然后创建空的 xib 资源并添加 Collection View 单元格 enter image description here enter image description here enter image description here

并设置该控件的类名和标识符 enter image description here

然后创建委托(delegate)方法将自定义单元添加到您的 collectionViewController

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

    static NSString *cellIdentifier = @"customCell";

    customCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    return cell;

}

关于ios - UICollectionView 单元格未添加为 UICollectionView 的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20994378/

相关文章:

ios - 在哪里保存游戏分数、最后通过的关卡等? Parse(云后端)还是 Facebook?

objective-c - UIWebView 不会在单独的类文件中加载请求

iphone - 如何在 Cocoa 中将一张图像与另一张图像的 alpha 值相乘?

iOS - 使用 PresentViewController 呈现 ViewController : doesn't work

iphone - 有没有办法在 Xcode 的 resources 文件夹中创建子文件夹?

objective-c - 为什么我应该使用 Aspect Ratio NSLayoutConstraint?

objective-c - NSUUID 可以通过继承来扩展吗?如何?

ios - 具有相关 Objective-C Pod 的 Swift Pod 的桥接头?

ios - 如何从 AVCaptureVideoPreviewLayer 获取 UIImage?

ios - 用于管理观察者的 Objective-C 设计模式