【发布时间】:2020-10-05 15:35:19
【问题描述】:
我正在设计一个带有collectionview的菜单标签栏,我想在标签被选中时改变它的颜色。
一切正常,但是当所选项目不再出现在屏幕上时(由于滚动到屏幕外),didDeselectItemAt 里面的函数就不再工作了。
有没有办法解决这个问题?下面是代码:
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if collectionView == self.productMenuCollectionView {
guard let cell = self.productMenuCollectionView.cellForItem(at: indexPath) as? ProductMenuCollectionViewCell else {
return
}
cell.label.textColor = UIColor.black
} else {
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == self.productMenuCollectionView {
let cell = self.productMenuCollectionView.cellForItem(at: indexPath) as! ProductMenuCollectionViewCell
cell.label.textColor = CustomColor.primary
} else {
}
}
【问题讨论】:
-
更可靠的方法是将
isSelected状态保存在数据模型中并重新加载行。