【问题标题】:UICollectionView cell.ViewWithTag returning nil for UILabelUICollectionView cell.ViewWithTag 为 UILabel 返回 nil
【发布时间】:2015-08-03 07:44:11
【问题描述】:

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

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...”。任何帮助将不胜感激,谢谢!

【问题讨论】:

标签: ios swift uicollectionview uilabel viewwithtag


【解决方案1】:

尝试从 viewDidLoad 中删除这一行:

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

【讨论】:

    【解决方案2】:

    我刚刚使用您的代码创建了一个示例项目,对我来说它可以工作。虽然您不应该强制打开包装。

    确保故事板中的设置正确:

    检查您的 collectionViewCell 是否设置正确:

    并设置标签的标签:

    这里是sample project

    【讨论】:

    • 您的示例项目适合我。我重新创建了一个 VC 并在其中放置了一个 CollectionView,将其连接到委托和数据源,在标签上设置我的标签以及单元格中的其他几个按钮,但是,我仍然收到错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多