解码后如何恢复 UICollectionViewCell 中的绑定
How to restore bindings in UICollectionViewCell after it is decoded
我有 UICollectionViewCell
的子class。该单元格是在故事板中视觉设计的,包含许多组件,这些组件使用故事板绑定到 Swift subclass 中的变量。
Swift class 只是提供了从数据源检索到的数据填充组件的逻辑。
例如:
class InfoCollectionViewCell : UICollectionViewCell {
@IBOutlet weak var mainPanel : UIView!
@IBOutlet weak var panel1 : UIView!
@IBOutlet weak var firstName : UILabel!
@IBOutlet weak var lastName : UILabel!
@IBOutlet weak var address : UILabel!
etc ...
func setVariousProperties(etc) {
firstName.text = ... etc
数据源做通常的事情:
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("InfoCell", forIndexPath:indexPath)
if let c : InfoCollectionViewCell = cell as? InfoCollectionViewCell {
c.setVariousProperties(...)
}
我最初只实现了包含 "not implemented" 断言的 decode/encode 方法,但很明显框架偶尔会编码和解码 class。我在没有保存组件的情况下实现了虚拟 encode/decode 方法,导致在访问组件时出现 nil 值的可预测问题。
所以看来我要么一个一个实现UICollectionViewCell
子class中的所有控件的编码和解码,要么我必须找到更好的方法。
这似乎是浪费时间,因为我实际上并不需要(我不认为)保存组件的内容,因为它们无论如何都将被 subclass: 我将用数据源中的值覆盖组件内容。
显然,所有控件都在故事板中定义。我可以在 init 方法中按名称从情节提要中手动获取它们,但这似乎同样乏味,并且使控件和变量的图形链接变得多余。
有没有更好的方法?
我可以直接说 "restore connections" 或类似的话吗?
编辑:
在发布问题和添加赏金之间的某个时间,问题不再发生。我现在注意到我的组件的编码方法没有被调用。所以出于某种原因,框架决定序列化我的对象并反序列化它们,但现在不是。因此问题没有发生,我无法提供堆栈跟踪。
可以想象 XCode 的一些更新已经解决了这个问题,或者可能是其他问题。
我显然仍然担心某处潜伏着一些错误。
您不需要为情节提要中布置和连接的任何控件实现 encoding/decoding。这将在运行时得到处理,只要其他所有内容都已正确连接并准备好使用 UI 组件,您将在 collectionView.dequeueReusableCellWithReuseIdentifier
之后得到一个单元格。要检查的内容是:
- 子class在故事板自定义class部分中指定
- 定义了 reuseIdentifier 以匹配您期望的字符串
- 所有 IBOutlet 都已连接,并且还应在 IBOutlet var 行旁边的代码中显示连接。
一旦您确定这些是正确的,请尝试打印出组件以检查它们是否仍然为零,并在其中放置一些简单的文本以消除任何数据源问题:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("InfoCell", forIndexPath:indexPath)
if let c = cell as? InfoCollectionViewCell{
print(c.address)
print(c.firstName)
print(c.lastName)
c.firstName.text = "firstName Test"
c.lastName.text = "lastName Test"
c.address.text = "address Test"
}
return cell
}
}
我有 UICollectionViewCell
的子class。该单元格是在故事板中视觉设计的,包含许多组件,这些组件使用故事板绑定到 Swift subclass 中的变量。
Swift class 只是提供了从数据源检索到的数据填充组件的逻辑。
例如:
class InfoCollectionViewCell : UICollectionViewCell {
@IBOutlet weak var mainPanel : UIView!
@IBOutlet weak var panel1 : UIView!
@IBOutlet weak var firstName : UILabel!
@IBOutlet weak var lastName : UILabel!
@IBOutlet weak var address : UILabel!
etc ...
func setVariousProperties(etc) {
firstName.text = ... etc
数据源做通常的事情:
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("InfoCell", forIndexPath:indexPath)
if let c : InfoCollectionViewCell = cell as? InfoCollectionViewCell {
c.setVariousProperties(...)
}
我最初只实现了包含 "not implemented" 断言的 decode/encode 方法,但很明显框架偶尔会编码和解码 class。我在没有保存组件的情况下实现了虚拟 encode/decode 方法,导致在访问组件时出现 nil 值的可预测问题。
所以看来我要么一个一个实现UICollectionViewCell
子class中的所有控件的编码和解码,要么我必须找到更好的方法。
这似乎是浪费时间,因为我实际上并不需要(我不认为)保存组件的内容,因为它们无论如何都将被 subclass: 我将用数据源中的值覆盖组件内容。
显然,所有控件都在故事板中定义。我可以在 init 方法中按名称从情节提要中手动获取它们,但这似乎同样乏味,并且使控件和变量的图形链接变得多余。
有没有更好的方法?
我可以直接说 "restore connections" 或类似的话吗?
编辑:
在发布问题和添加赏金之间的某个时间,问题不再发生。我现在注意到我的组件的编码方法没有被调用。所以出于某种原因,框架决定序列化我的对象并反序列化它们,但现在不是。因此问题没有发生,我无法提供堆栈跟踪。
可以想象 XCode 的一些更新已经解决了这个问题,或者可能是其他问题。
我显然仍然担心某处潜伏着一些错误。
您不需要为情节提要中布置和连接的任何控件实现 encoding/decoding。这将在运行时得到处理,只要其他所有内容都已正确连接并准备好使用 UI 组件,您将在 collectionView.dequeueReusableCellWithReuseIdentifier
之后得到一个单元格。要检查的内容是:
- 子class在故事板自定义class部分中指定
- 定义了 reuseIdentifier 以匹配您期望的字符串
- 所有 IBOutlet 都已连接,并且还应在 IBOutlet var 行旁边的代码中显示连接。
一旦您确定这些是正确的,请尝试打印出组件以检查它们是否仍然为零,并在其中放置一些简单的文本以消除任何数据源问题:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("InfoCell", forIndexPath:indexPath)
if let c = cell as? InfoCollectionViewCell{
print(c.address)
print(c.firstName)
print(c.lastName)
c.firstName.text = "firstName Test"
c.lastName.text = "lastName Test"
c.address.text = "address Test"
}
return cell
}
}