【发布时间】:2018-08-30 14:21:38
【问题描述】:
我有一个作为 UICollectionViewController 实现的滑出式菜单。我也为集合视图创建了自定义单元格。导航和一切都按预期工作。当我单击实际单元格时,我遇到的问题是更改单元格的外观。
我已经尝试了几种基于解决方案的方法(1)(2) 我在这里看到过堆栈,但没有什么令我满意。
解决方案 1:实现 UICollectionViewController 委托方法:
class SlideOutMenuViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout{
//Setup code and other delegate methods….
override func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! SlideOutMenuCells
cell.backgroundColor = .white
}
override func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! SlideOutMenuCells
cell.backgroundColor = UIColor.mainGreen()
}
}
当我尝试此解决方案时,没有任何反应。单元格背景颜色不会改变颜色。
解决方案 2:此解决方案会产生更好的结果,但当我握住单元格时,单元格会改变颜色。我希望单元格的背景颜色在 tap 上快速闪烁或突出显示,而不仅仅是用户按住单元格时。
class SlideOutMenuCells: UICollectionViewCell {
//Setup code...
override var isHighlighted: Bool {
didSet {
if self.isHighlighted {
backgroundColor = UIColor.darkGreen()
} else {
backgroundColor = UIColor.mainGreen()
}
}
}
}
这两种解决方案都没有真正按预期工作。我在这里看到了几篇尝试解决这个问题的帖子,但还没有找到一个真正有效的解决方案。我希望单元格通过点击闪烁突出显示,而不仅仅是当用户单击并按住单元格时......
【问题讨论】:
-
您可以尝试更改 contentView 的背景颜色吗?
-
@AbhishekHarsha 这不起作用。我已经尝试过,因为我在其他一些解决方案中看到了它,但没有运气。
-
@mufc 请检查我的回答也许它会解决你的问题。
-
您已在某些地方管理所选项目,因为集合视图重用了可能出现问题的单元格
标签: ios swift uicollectionview uicollectionviewcell