Ios 轻触两次以在集合视图中高亮显示复选标记
我有一个集合视图,您可以点击单元格放大图像,点击放大图像将其关闭,在每个单元格上我还有一个可选择的复选标记,以便您可以选择稍后下载所选图像。代码几乎可以正常工作,但有时我必须选择或取消选择复选标记两次。我真的不明白我遗漏了什么,但这是我的代码:Ios 轻触两次以在集合视图中高亮显示复选标记,ios,swift,swift3,uicollectionview,Ios,Swift,Swift3,Uicollectionview,我有一个集合视图,您可以点击单元格放大图像,点击放大图像将其关闭,在每个单元格上我还有一个可选择的复选标记,以便您可以选择稍后下载所选图像。代码几乎可以正常工作,但有时我必须选择或取消选择复选标记两次。我真的不明白我遗漏了什么,但这是我的代码: func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { l
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as! PhotoCell
let tap = UITapGestureRecognizer(target: self, action: #selector(checkmarkWasTapped(_ :)))
cell.backgroundColor = .clear
cell.imageView.image = UIImage(contentsOfFile: imagesURLArray[indexPath.row].path)
cell.checkmarkView.checkMarkStyle = .GrayedOut
cell.checkmarkView.tag = indexPath.row
cell.checkmarkView.addGestureRecognizer(tap)
return cell
}
func checkmarkWasTapped(_ sender: UIGestureRecognizer) {
let checkmarkView = sender.view as! SSCheckMark
let indexPath = IndexPath(row: checkmarkView.tag, section: 0)
let imageURL = imagesURLArray[indexPath.row]
if checkmarkView.checked == true {
checkmarkView.checked = false
selectedImagesArray.remove(at: selectedImagesArray.index(of: imageURL)!)
} else {
checkmarkView.checked = true
selectedImagesArray.append(imageURL)
}
collectionView.reloadItems(at: [indexPath])
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
addZoomedImage(indexPath.row)
addGestureToImage()
addBackGroundView()
view.addSubview(selectedImage)
}
任何帮助都会很好。谢谢我相信当您在collection view上滚动时会出现问题,collection view会重新加载每个单元格,并且您没有关于checkview最新状态的任何信息 通常我存储一个bool数组(集合视图中元素数量的大小,加载模型后可以轻松创建),因此每次用户双击复选标记视图时,我都会更改集合视图单元格的索引,并将bool数组元素更改为true
var boolArray = [Bool]()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as! PhotoCell
let tap = UITapGestureRecognizer(target: self, action: #selector(checkmarkWasTapped(_ :)))
cell.backgroundColor = .clear
cell.imageView.image = UIImage(contentsOfFile: imagesURLArray[indexPath.row].path)
if(boolArray[indexPath.row]){
cell.checkmarkView.checked = false
}
else{
cell.checkmarkView.checked = true
}
cell.checkmarkView.checkMarkStyle = .GrayedOut
cell.checkmarkView.tag = indexPath.row
cell.checkmarkView.addGestureRecognizer(tap)
return cell
}
这可能是一个丑陋的解决方案,但我相信它会起作用。您好,谢谢您的回复,您添加的if语句是否会在每次重新加载时交换所选内容?它应该说
如果!(boolArray[indexPath.row]){
如果用户在boolArray[indexPath.row]之前没有为单元格添加书签,那么在加载单元格时将取消选中checkmarkView。否则它将被选中。如果您的checkmarkView是这样工作的?如果我在viewdidload中用false填充bool数组并使用If!(boolArray[indexPath.row])
它们都是未选中的,但是当我选择检查时,然后立即取消选中,如果我使用if(boolArray[indexPath.row])
执行相同的操作,那么它们都会被选中,并且在我选择它们时不会发生任何事情。啊,我需要更新checkmarkwasttapped()中的bool数组
并使用if!(boolArray[indexPath.row])
现在它可以工作了。感谢您的帮助!