UICollectionViewCell 注册类失败,但注册笔尖有效 [英] UICollectionViewCell register class fails, but register nib works

查看:37
本文介绍了UICollectionViewCell 注册类失败,但注册笔尖有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为我的 UICollectionViewController 创建一个自定义的 UICollectionViewCell,它通过注册笔尖来工作;但是,无法按班级注册.

使用 registerClass() - 失败

按类注册似乎是正确的 - 它构建,但在运行时从 UIView 解包可选元素时抛出异常.没有引用网点,它运行;但是,集合视图中不会出现任何单元格.

collectionView?.registerClass(MyCollectionViewCell.self,forCellWithReuseIdentifier: "myCell")

显然没有加载视图;但是,我认为设置单元格的自定义类会提供必要的链接.

使用 registerNib() - 有效

通过 nib 注册有效,这是有道理的,因为它显式加载视图.

let nib = UINib(nibName: "MyCollectionViewCell", bundle: nil)collectionView?.registerNib(nib, forCellWithReuseIdentifier: "myCell")

这里显式引用了视图,并为视图设置了自定义类;然而,这似乎有些不对劲.

示例项目

在示例项目中隔离问题,下面是要复制的视图和代码;或者,此项目

MyCollectionViewController.swift

显示按笔尖和按类别注册:

导入 UIKit类 MyCollectionViewController: UICollectionViewController {var 数据 = [字符串]()覆盖 func viewDidLoad() {super.viewDidLoad()数据 = ["a", "b", "c"]//这有效,由笔尖让 nib = UINib(nibName: "MyCollectionViewCell", bundle: nil)collectionView?.registerNib(nib, forCellWithReuseIdentifier: "myCell")//这失败了,按类//collectionView?.registerClass(MyCollectionViewCell.self, forCellWithReuseIdentifier: "myCell")}覆盖 func collectionView(collectionView: UICollectionView, numberOfItemsInSection 部分: Int) ->整数{返回数据.count}覆盖 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) ->UICollectionViewCell {let cell = collectionView.dequeueReusableCellWithReuseIdentifier("myCell", forIndexPath: indexPath) as!MyCollectionViewCellcell.title.text = 数据[indexPath.row]返回单元格}}

MyCollectionViewCell.xib

View 是一个 UICollectionViewCell,子类为 MyCollectionViewCell,带有 UILabel:

在我的笔尖中,Collection Reusable View Identifier 已设置为:myCell:

MyCollectionViewCell.swift

定义类,并且标签有一个IBOutlet:

导入 UIKit类 MyCollectionViewCell: UICollectionViewCell {@IBOutlet 弱变量标题:UILabel!}

使用笔尖,执行显示为:

为什么我不能按类注册UICollectionViewCell?

同样,我对原型单元是否需要保留在主故事板集合视图控制器上有点模糊.在那里,我没有定义任何可重用的视图标识符.

解决方案

我看到这个链接 概览合集查看

<块引用>

如果cell类是用代码编写的,则使用UICollectionView的registerClass:方法进行注册.例如:[self.myCollectionView registerClass:[MyCollectionViewCell 类] forCellWithReuseIdentifier:@"MYCELL"];如果单元包含在 Interface Builder NIB 文件中,则使用 registerNib: 方法代替.

所以你的收藏单元是用nib创建的,你应该用nib注册.如果您的单元格完全用代码编写,则需要在课堂上注册.

希望对您有所帮助.

Creating a custom UICollectionViewCell for my UICollectionViewController, it works by registering the nib; however, fails to register by class.

Using registerClass() - fails

Registering by class seems correct - it builds, but throws an exception at runtime unwrapping optional elements from the UIView. Without referencing outlets, it runs; however, no cells appear within the collection view.

collectionView?.registerClass(MyCollectionViewCell.self, 
                              forCellWithReuseIdentifier: "myCell")

Clearly the view is not loaded; however, I'd think setting the custom class of the cell would provide necessary linkage.

Using registerNib() - works

Registering by nib works, which makes sense as it is explicitly loading the view.

let nib = UINib(nibName: "MyCollectionViewCell", bundle: nil)
collectionView?.registerNib(nib, forCellWithReuseIdentifier: "myCell")

Here the view is explicitly referenced, and custom class is set for the view; however, something about this seems wrong.

Sample project

Isolating the issue in a sample project, below are views and code to replicate; or, this project is available at GitHub.

Main.storyboard

My main storyboard initial view controller is a UICollectionViewController subclassed as MyCollectionViewController:

MyCollectionViewController.swift

Showing both registering by nib, and by class:

import UIKit

class MyCollectionViewController: UICollectionViewController {

    var data = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        data = ["a", "b", "c"]

        // This works, by nib
        let nib = UINib(nibName: "MyCollectionViewCell", bundle: nil)
        collectionView?.registerNib(nib, forCellWithReuseIdentifier: "myCell")

        // This fails, by class
        //collectionView?.registerClass(MyCollectionViewCell.self, forCellWithReuseIdentifier: "myCell")
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.count
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("myCell", forIndexPath: indexPath) as! MyCollectionViewCell

        cell.title.text = data[indexPath.row]

        return cell
    }

}

MyCollectionViewCell.xib

View is a UICollectionViewCell subclassed as MyCollectionViewCell with a UILabel:

In my nib the Collection Reusable View Identifier has been set to: myCell:

MyCollectionViewCell.swift

Defines the class, and has an IBOutlet to the label:

import UIKit

class MyCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var title: UILabel!

}

Using the nib, execution appears as:

Why can't I register UICollectionViewCell by class?

As well, I'm a little fuzzy as to whether the prototype cell needs to remain on the main storyboard collection view controller. There, I have not defined any reusable view identifier.

解决方案

I see this link Overview Collection View

If the cell class was written in code, the registration is performed using the registerClass: method of UICollectionView. For example: [self.myCollectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"MYCELL"]; In the event that the cell is contained within an Interface Builder NIB file, the registerNib: method is used instead.

So your collection cell create with nib, you should register with nib. If your cell written totally in code you will need register with class.

Hope this help.

这篇关于UICollectionViewCell 注册类失败,但注册笔尖有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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