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

好的感謝這是好主意 –

相關問題