【发布时间】:2019-02-27 20:48:29
【问题描述】:
我有一个双向滚动的collectionView,我希望一个单元格背景视图在选中时出现,在取消选中时消失。 我声明了一个带有选定 indexPaths 的数组以在 cellForItemAt indexPath func 中对其进行测试,但它不起作用。请帮忙! 这是我的代码:
var selectedIndexPaths:[IndexPath]=[]
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if(selectedIndexPaths.contains(indexPath)){
selectedIndexPaths=selectedIndexPaths.filter { $0 != indexPath }
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedIndexPaths.append(indexPath)
collectionView.reloadItems(at: [indexPath])
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// swiftlint:disable force_cast
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: contentCellIdentifier,
for: indexPath) as! UserCollectionCell
if(selectedIndexPaths.contains(indexPath) ){
cell.backgroundView?.isHidden=false
}
else{
cell.backgroundView?.isHidden=true
}
return cell
}
【问题讨论】:
标签: swift collectionview