UICollectionView cell.ViewWithTag 为 UILabel 返回 nil [英] UICollectionView cell.ViewWithTag returning nil for UILabel

查看:14
本文介绍了UICollectionView cell.ViewWithTag 为 UILabel 返回 nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个标签似乎返回 nil,即使我正确设置了重用标识符和标签.

This label seems to return nil, even though I have the reuseIdentifier and tag set properly.

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var identifier: String = "CollectionCell"
    var cell: UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! UICollectionViewCell

    // Configure the cell


    //save till later, when images are actually present

    //var cellItem1 = hostManager[indexPath.row * 2]

    let label:UILabel = cell.viewWithTag(1) as! UILabel
    return cell

}

程序在标签设置为 viewWithTag 的地方中断.我没有为单元设置自定义类,只有原型.标签设置在情节提要上.收到错误EXC_BAD_INSTRUCTION...".任何帮助将不胜感激,谢谢!

The program breaks where the label is set = to the viewWithTag. I have no custom class set for the cell, just the prototype. The tag is set on the storyboard. Getting an error "EXC_BAD_INSTRUCTION...". Any help would be appreciated, Thanks!

推荐答案

尝试从 viewDidLoad 中删除这一行:

Try removing this line from viewDidLoad:

self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

这篇关于UICollectionView cell.ViewWithTag 为 UILabel 返回 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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