CellForItematIndexPath 无法设置变量
CellForItematIndexPath cannot set variable
我现在正在测试的 UICollectionViewCell 中有一个变量...这是我的 cellforitem
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let c = collectionView.dequeueReusableCell(withReuseIdentifier: chatCellId, for: indexPath) as! ChatCell
c.userDidSend = (indexPath.item % 2 == 0) ? true : false
return c
}
在 ChatCell class 中,我使用 var userDidSend: Bool?
初始化变量
,但是每当我测试变量时,它总是 returns nil。下面是为每个单元格测试变量的位置。
override func setupViews() {
super.setupViews()
if userDidSend == true {
setupSentChat()
} else if userDidSend == false {
setupRecievedChat()
}
print(userDidSend)
}
除此特定问题外,一切正常。
想法?
编辑:
初始化单元格视图时调用 setupViews。
override init(frame: CGRect){
super.init(frame: frame)
setupViews()
}
使用自定义 setter 似乎已经解决了这个问题。
var userDidSend: Bool? {
didSet {
if userDidSend == true {
setupSentChat()
} else if userDidSend == false {
setupRecievedChat()
}
}
}
我现在正在测试的 UICollectionViewCell 中有一个变量...这是我的 cellforitem
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let c = collectionView.dequeueReusableCell(withReuseIdentifier: chatCellId, for: indexPath) as! ChatCell
c.userDidSend = (indexPath.item % 2 == 0) ? true : false
return c
}
在 ChatCell class 中,我使用 var userDidSend: Bool?
初始化变量
,但是每当我测试变量时,它总是 returns nil。下面是为每个单元格测试变量的位置。
override func setupViews() {
super.setupViews()
if userDidSend == true {
setupSentChat()
} else if userDidSend == false {
setupRecievedChat()
}
print(userDidSend)
}
除此特定问题外,一切正常。 想法?
编辑: 初始化单元格视图时调用 setupViews。
override init(frame: CGRect){
super.init(frame: frame)
setupViews()
}
使用自定义 setter 似乎已经解决了这个问题。
var userDidSend: Bool? {
didSet {
if userDidSend == true {
setupSentChat()
} else if userDidSend == false {
setupRecievedChat()
}
}
}