如何解决CollectionView 第一个和最后一个单元格保持选中状态
我正在使用一个可选择的 collectionView,当用户选择第一个然后最后一个单元格时,由于某些原因,两个单元格都保持选中状态。
func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath)
{
if (collectionView == self.categoryCollectionView)
{
let cell = collectionView.cellForItem(at: indexPath) as? ChannelsCategoryCell
cell?.triangleIndicatorView.isHidden = false
cell?.categoryLabel.textColor = UIColor.init(named: "ActiveSymbol")
loadChannelList()
}
func collectionView(_ collectionView: UICollectionView,didDeselectItemAt indexPath: IndexPath)
{
if (collectionView == self.categoryCollectionView)
{
let cell = collectionView.cellForItem(at: indexPath) as? ChannelsCategoryCell
cell?.triangleIndicatorView.isHidden = true
cell?.categoryLabel.textColor = UIColor.init(named: "InactiveCategoryText")
}
解决方法
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier,for: indexPath) as! CollectionViewCell
if selectedIndex == indexPath.item {
cell.backgroundColor = .blue
} else {
cell.backgroundColor = .clear
}
}
func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
selectedIndex = indexPath.item
collectionView.reloadData()
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。