【问题标题】:Why is UICollectionViewCell's outlet nil?为什么 UICollectionViewCell 的出口为零?
【发布时间】:2014-09-29 16:08:22
【问题描述】:

我在 Interface Builder 中创建了一个自定义 UICollectionViewCell,将其上的视图绑定到类,然后当我想使用字符串并将其设置为字符串上的标签时,该标签的值为 nil。

override func viewDidLoad() {
    super.viewDidLoad()

    // Register cell classes
    self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "ls")
}

override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {

    var cell: LeftMenuCollectionViewCell
    cell = collectionView.dequeueReusableCellWithReuseIdentifier("ls", forIndexPath: indexPath) as LeftMenuCollectionViewCell
    println(cell.label) // <- this is nil, why??
    cell.label.text = "asd"

    return cell
}

以及子类单元格:

class LeftMenuCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!
}

【问题讨论】:

  • 您使用显式“!”是否有原因?除了到处都是类型名称?似乎是多余的,尤其是在 IBOutlets 中。唯一可能重要的地方是您将单元格作为 LeftmenuCollectionViewCell 出列,但您不在那里执行此操作。您可以 println 或 NSLog 'cell' 本身吗?
  • 如果我删除“!”或使用“?”在类定义中,我得到编译器错误或崩溃。 “!”是绑定时的默认值。
  • ant 单元格本身:>
  • 所有网点都连接到 IB?其他网点有用吗?没有看到任何其他明显的问题,抱歉。

标签: ios swift interface-builder ios8 uicollectionviewcell


【解决方案1】:

你没有注册你的手机,

fileprivate let yourIdentifier = ""yourIdentifier"
super.viewDidLoad() { 

//这里需要注册cell collectionView?.register(NameOfYourClass.self, forCellWithReuseIdentifier: "yourIdentifier") }

【讨论】:

    【解决方案2】:

    我认为最好的解决方案是直接从情节提要中使用添加CollectionView,或者您需要从情节提要中的CollectionView 中删除CollectionViewCell,并使用以下命令注册一个单元格:

    collectionView?.register(UINib(nibName: "YourItemClassName", bundle: nil), forCellWithReuseIdentifier: "yourIdentifier")

    【讨论】:

      【解决方案3】:

      看起来有两种注册方法,我第一个用错了。 我有一个自定义 xib 视图,所以注册了第二个选项,我们有数据!

      1:

      collectionView?.register(YourItemClassName.self, forCellWithReuseIdentifier: "yourIdentifier") 
      

      2:

      collectionView?.register(UINib(nibName: "YourItemClassName", bundle: nil), forCellWithReuseIdentifier: "yourIdentifier")
      

      【讨论】:

        【解决方案4】:

        如果您使用的是 xib,请确保已将这行代码添加到您的 viewdidload 中。

        目标 C:

        [self.collectionView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellWithReuseIdentifier:@"MyCellIdentifier"];
        

        斯威夫特:

        collectionView.register(UINib(nibName:"MyCell", bundle: nil), forCellWithReuseIdentifier:"MyCellIdentifier")
        

        【讨论】:

        • 是必须同时注册class和nib,还是只有nib就够了?
        • 我刚刚注册了笔尖,它解决了我的问题。
        • 看起来有两种注册方法,我使用了错误的一种... collectionView?.register(UINib(nibName: "YourItemClassName", bundle: nil), forCellWithReuseIdentifier: "yourIdentifier") collectionView ?.register(YourItemClassName.self, forCellWithReuseIdentifier: "yourIdentifier")
        【解决方案5】:

        必须注册那个笔尖的家伙!

        collectionView.register(UINib(nibName: "CustomCell", bundle: nil), forCellWithReuseIdentifier: "CustomCellId")
        

        【讨论】:

          【解决方案6】:

          只需删除这一行:

          self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "ls")
          

          【讨论】:

          • 我不明白为什么这被否决了。这就是我的问题的答案。
          • 这是正确的答案,应该由提问的人做相应的标记。也很高兴了解它为什么起作用:)
          • 猜它被否决了,因为这个问题是在 2014 年 8 月自己回答的......而这个答案只是在几个月后在这里再次复制答案。
          • 但是当我不使用它时,它会给我错误 Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:]
          • 欢迎解释为什么不需要这条线。它目前由 Xcode 样板添加用于新的集合视图。
          【解决方案7】:

          我遇到了类似的问题,但我的错误是我没有委托 CollectionViewCell 来更改标签文本。

          【讨论】:

            【解决方案8】:

            我再次致电self.collectionView.registerClass(LeftMenuCollectionViewCell.self, forCellWithReuseIdentifier: "ls")如果您使用的是故事板,您不想调用它。它将覆盖您在故事板中的内容。

            如果您仍有问题,请检查reuseIdentifier 是否相同dequeueReusableCellWithReuseIdentifier storyboard

            【讨论】:

            • 谢谢!与使用自定义单元格一样,花了一个多小时。
            • 我也有同样的问题,但从未使用过这行代码,你知道为什么我会得到同样的异常吗?
            • 谢谢。这为我解决了。我有一个故事板,我手动添加了一个视图控制器。样板代码包括您提到的那一行。即使它看起来是正确的(正确的标识符和类别),它也拒绝连接插座。现在它可以工作了。甜!
            • 如果您使用的是情节提要,则不想调用它。它会覆盖你在故事板中的内容。
            • GAH,UICollectionViewController 模板中有什么可怕的东西!!!!!!谢谢,杰诺斯。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-02-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多