我有一个带有两个海关单元的集合视图,我有一种方法,当选择一个单元格时,选定的单元格上出现一个边框,以及一个取消所有选择的按钮,问题是当按下按钮时,单元格边框被设置为清除颜色但是当再次进入选择模式时,先前选择的单元格仍然具有边框,所以我正在寻找的是当我取消选择然后再次进入选择模式时不应该选择单元格,这里是代码:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if isGridSelected {
let cell:cell2_Class = collectionView.dequeueReusableCellWithReuseIdentifier("cell2", forIndexPath: indexPath) as! cell2_Class
collectionView.allowsMultipleSelection = true
collectionView.allowsSelection = true
cell.listImage.image = imageArray[indexPath.row]
if self.selectedIndexes.indexOf(indexPath) == nil || cancel == true {
cell.layer.borderColor = UIColor.clearColor().CGColor
// Unselected
}
else if cancel == true { cell.layer.borderColor = UIColor.clearColor().CGColor
for indexPath: NSIndexPath in selectedIndexes {
self.collectionView.deselectItemAtIndexPath(indexPath, animated: false)
collectionView.reloadData()
}
} else {
cell.layer.borderColor = UIColor.greenColor().CGColor // Selected
cell.layer.borderWidth = 3
}
return cell
}
}
var flag = false
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if flag == false{ self.performSegueWithIdentifier("showimage", sender: self) }
else if cancel == true {
let cell = collectionView.cellForItemAtIndexPath(indexPath)
cell?.selected = false
// deselect
} else if flag == true {
if let indexSelection = selectedIndexes.indexOf(indexPath) {
selectedIndexes.removeAtIndex(indexSelection)
} else {
selectedIndexes.append(indexPath)
}
self.collectionView.reloadData()
}
}
func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
if gestureReconizer.state != UIGestureRecognizerState.Began {
return
}
let p = gestureReconizer.locationInView(self.collectionView)
let indexPath = self.collectionView.indexPathForItemAtPoint(p)
if indexPath != nil {
if let indexSelection = selectedIndexes.indexOf(indexPath!) {
selectedIndexes.removeAtIndex(indexSelection)
} else {
selectedIndexes.append(indexPath!)
}
print(indexPath?.row)
cancel = false
flag = true
self.title = "share photos"
cancelButton.hidden = false
} else {
}
}
答案 0 :(得分:0)
当您按cancel
时,self.collectionView.deselectItemAtIndexPath(indexPath, animated: false)
为indexPaths
中的所有self.selectedIndexPaths
拨打self.selectedIndexPaths
,看来我的代码中没有看到此从else if cancel == true {
self.selectedIndexes.removeAllObjects()
collectionView.reloadData()
}
删除,是吗?
相反尝试这个
UICollectionView
我还必须指出variable
有一个名为indexPathsForSelectedItems: [IndexPath]?
的私有setSelected()
可用于此类实施,并与UICollectionViewCell
中的lighter
重叠使用{1}}可以使您的代码变得更加Main.storyboard
。个人偏好。
答案 1 :(得分:0)
我没有那么多用来迅速,但它完全合乎逻辑,所以我可以给你一个想法
注意:如果数组包含indexPath,则添加check in cellForRow方法,然后添加其他边界
按下取消时,您可以清空数组并重新加载集合视图