0

我正在使用swift 3,并在我的应用程序中显示iPhone图库的集合视图。每一件事情都很好但我希望用户可以选择一些照片,这可能会发生但问题是我在每个单元格中使用另一个小图像,当用户选择该单元格时,图片将显示一个勾号,用户可以理解该单元格已被选中 - 这将在开始时起作用,但当用户滚动收集视图时,某些用户没有选择它们的单元格也具有此刻度! **请记住,选择和取消选择运作良好,问题只是为了显示此勾号** **我有单元格中的标记的图像,该图像将显示一个勾或没有什么时,用户选择单元格或didDeselect那**为什么集合视图中的选定单元格在UICollection View Swift 3中显示不正确?

我知道这些代码是比你想象的多,但有些人需要访问所有的代码,所以你可以看到下面

import UIKit 
import Photos 
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { 
let arr_img = NSMutableArray() 

@IBOutlet var collview: UICollectionView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    let allPhotosOptions : PHFetchOptions = PHFetchOptions.init() 
    allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)] 
    let allPhotosResult = PHAsset.fetchAssets(with: .image, options: allPhotosOptions) 
    allPhotosResult.enumerateObjects({ (asset, idx, stop) in 

     self.arr_img.add(asset) 
    }) 

} 
func getAssetThumbnail(asset: PHAsset, size: CGFloat) -> UIImage { 
    let retinaScale = UIScreen.main.scale 
    let retinaSquare = CGSize(width: size * retinaScale, height: size * retinaScale)//CGSizeMake(size * retinaScale, size * retinaScale) 
    let cropSizeLength = min(asset.pixelWidth, asset.pixelHeight) 
    let square = CGRect(x: 0, y: 0, width: cropSizeLength, height: cropSizeLength)//CGRectMake(0, 0, CGFloat(cropSizeLength), CGFloat(cropSizeLength)) 
    let cropRect = square.applying(CGAffineTransform(scaleX: 1.0/CGFloat(asset.pixelWidth), y: 1.0/CGFloat(asset.pixelHeight))) 

    let manager = PHImageManager.default() 
    let options = PHImageRequestOptions() 
    var thumbnail = UIImage() 

    options.isSynchronous = true 
    options.deliveryMode = .highQualityFormat 
    options.resizeMode = .exact 
    options.normalizedCropRect = cropRect 

    manager.requestImage(for: asset, targetSize: retinaSquare, contentMode: .aspectFit, options: options, resultHandler: {(result, info)->Void in 
     thumbnail = result! 
    }) 
    return thumbnail 
} 
override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

//MARK: 
//MARK: Collectioview methods 
func collectionView(_ collectionView: UICollectionView, 
        numberOfItemsInSection section: Int) -> Int { 
    return arr_img.count 
} 
func collectionView(_ collectionView: UICollectionView, 
        cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", 
                for: indexPath) 
    let imgview : UIImageView = cell.viewWithTag(20) as! UIImageView 
    imgview.image = self.getAssetThumbnail(asset: self.arr_img.object(at: indexPath.row) as! PHAsset, size: 150) 

    return cell 
} 

} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 
{ 
let cell = collectionView.cellForItem(at: indexPath as IndexPath) 
    if cell!.isSelected == true { 


     print(indexPath) 
     cell?.backgroundColor = UIColor.orange 
     print("Cell is Selected!") 
     let selectView : UIImageView = cell?.viewWithTag(22) as! UIImageView 
     selectView.image = UIImage(named: "Select.png") 
    } 

    else if cell!.isSelected != true  { 

     collectionView.cellForItem(at: indexPath as IndexPath) 

    cell?.backgroundColor = UIColor.clear 
     let selectView : UIImageView = cell?.viewWithTag(22) as! UIImageView 
     selectView.image = nil 

     print("Cell is Not Selected!") 

    } 

} 

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) { 
    let cell = collectionView.cellForItem(at: indexPath as IndexPath) 

    print(indexPath) 
    if cell?.isSelected == true { 
     cell?.backgroundColor = UIColor.orange 
     print("Cell is Selected In Deselect!") 
     let selectView : UIImageView = cell!.viewWithTag(22) as! UIImageView 
     selectView.image = nil 
     collectionView.cellForItem(at: indexPath as IndexPath) 

    } 
    else if cell?.isSelected != true  { 
     cell?.backgroundColor = UIColor.clear 
     collectionView.cellForItem(at: indexPath as IndexPath) 

     print("Cell is DeSelected!") 
     let selectView : UIImageView = cell!.viewWithTag(22) as! UIImageView 
     selectView.image = nil 


    } 

} 
+0

这些细胞像的tableview细胞再利用。您需要维护一系列已选中的单元格,并在出现单元格后设置打勾图像。 –

+0

为什么写了didDeselectItemAt?因为你的代码只适用于didSelect。 – KKRocks

+0

单元格被重用。将一个属性isSelected添加到模型中,在'didSelect/didDeselect'中更新它,并根据'cellForItem'中的属性设置tick。 – vadian

回答

1

我的代码只需把你的单元格中的按钮,给它标签。

取数组并执行以下操作。

let arr_selected = NSMutableArray() 

func collectionView(_ collectionView: UICollectionView, 
         cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", 
                 for: indexPath) 

     let btn_check : UIButton = cell.viewWithTag(30) as! UIButton 
     if arr_selected.contains(indexPath.row){ 
      btn_check.isSelected = true 
     }else{ 
      btn_check.isSelected = false 
     } 
     return cell 
    } 
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){ 
     if arr_selected.contains(indexPath.row){ 
      arr_selected.remove(indexPath.row) 
     }else{ 
      arr_selected.add(indexPath.row) 
     } 
     self.collview.reloadData() 
    } 
如果你想演示

,那么你可以从HERE

+0

如何使用图像选择或取消选择按钮? –

+0

好吧,你想使用图像,然后采取imageview而不是按钮,所以当这种情况arr_selected.contains(indexPath.row)== true改变你的imageview作为选择其他取消选择@SaeedRahmatolahi –

+0

好的感谢这是好主意 –

相关问题