Swift - 子类 UICollectionViewCell 未加载 IBOutlet 属性
Swift - Subclassed UICollectionViewCell not loading IBOutlet properties
我来自 Obj-C,我正在努力在 Swift 中做一些超基础的事情!
我有一个习惯UICollectionViewCell
:
class CustomCell: UICollectionViewCell
{
// Outlets
// ***************************
@IBOutlet weak var button: UIButton!
// Init
// ***************************
required init?(coder aDecoder: NSCoder)
{
super.init(coder:aDecoder)
setup()
}
override init(frame: CGRect)
{
super.init(frame: frame)
setup()
}
func setup()
{
button.backgroundColor = .white
}
}
单元格是从外部 .xib 文件加载的,因此调用 init(coder:)
进行初始化,但我的按钮尚未准备好。
如果我更改为 button?.backgroundColor
,应用程序不会崩溃,但显然什么也没有发生。
我可以在 layoutSubviews()
中调用我的 setup()
函数并且它可以工作,但绝对不是正确的地方。
我该如何解决这个大问题?哈哈
编辑
可能我必须从 awakeFromNib()
调用 setup()
,对吗?
我一般不使用外部.xib,对它们不熟悉
编辑:抱歉,您似乎在我回答之前编辑了您的问题,您似乎是从 XIB 加载它,然后您可以 运行 awakeFromNib,当您 注册一个笔尖使用这个方法:
---旧post以下---
In Xcode 6 you have to provide additional init(coder:) initializer in
classes like RDCell, which is the subclass of UICollectionViewCell.
This initializer is called instead of init(frame:) when the class gets
initialized from a storyboard or a xib file. That’s not our case, but
we still need to provide init(coder:). We can use the solution
provided to us by Xcode. In Issue Navigator click on an error that
says “'required' initializer 'init(coder:)' must be provided by
subclass of 'UICollectionViewCell'“,
我来自 Obj-C,我正在努力在 Swift 中做一些超基础的事情!
我有一个习惯UICollectionViewCell
:
class CustomCell: UICollectionViewCell
{
// Outlets
// ***************************
@IBOutlet weak var button: UIButton!
// Init
// ***************************
required init?(coder aDecoder: NSCoder)
{
super.init(coder:aDecoder)
setup()
}
override init(frame: CGRect)
{
super.init(frame: frame)
setup()
}
func setup()
{
button.backgroundColor = .white
}
}
单元格是从外部 .xib 文件加载的,因此调用 init(coder:)
进行初始化,但我的按钮尚未准备好。
如果我更改为 button?.backgroundColor
,应用程序不会崩溃,但显然什么也没有发生。
我可以在 layoutSubviews()
中调用我的 setup()
函数并且它可以工作,但绝对不是正确的地方。
我该如何解决这个大问题?哈哈
编辑
可能我必须从 awakeFromNib()
调用 setup()
,对吗?
我一般不使用外部.xib,对它们不熟悉
编辑:抱歉,您似乎在我回答之前编辑了您的问题,您似乎是从 XIB 加载它,然后您可以 运行 awakeFromNib,当您 注册一个笔尖使用这个方法:
---旧post以下---
In Xcode 6 you have to provide additional init(coder:) initializer in classes like RDCell, which is the subclass of UICollectionViewCell. This initializer is called instead of init(frame:) when the class gets initialized from a storyboard or a xib file. That’s not our case, but we still need to provide init(coder:). We can use the solution provided to us by Xcode. In Issue Navigator click on an error that says “'required' initializer 'init(coder:)' must be provided by subclass of 'UICollectionViewCell'“,