选择单元格时显示复选标记按钮

Show checkmark button when the cell is selected

我有一个带有复选标记图像的按钮,我希望当用户选择单元格时,按钮从隐藏状态显示出来。

我已经配置了 collectionView,只需要为选定的单元格设置从隐藏状态显示复选标记按钮到选定状态的选项。

此外,如果有可能让复选标记按钮在加载数据时仅显示在第一个单元格上。

来自 VC 的代码:

var properties =  connectedProperties(StatusCode: 0)
var propertiesNew =  connectedProperties(StatusCode: 0)

override func viewDidLoad() {
    super.viewDidLoad()

    fetchAndReloadDataConnectedProperties()
}

func fetchAndReloadDataConnectedProperties(){
    APICallerGET.shared.connectedPropertiesOfAccount(for: APICallerGET.shared.token!) { [self] (result, error) in
        
        switch result?.StatusCode {
        case 0:
            
            self.propertiesNew = result!
            self.properties = self.propertiesNew
            
            DispatchQueue.main.async {
                result.first?.isSelected = true
                self.CollectionView.reloadData()
            }
        case 1:
            print("error")
        default:
            break
        }
    }
}


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
    return properties.Result?.count ?? 0
}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dashboardCollectionViewCell", for: indexPath) as? dashboardCollectionViewCell else { return UICollectionViewCell() }
    
    let currentPropertie = properties.Result?[indexPath.row]
    
    cell.nameSurnameLabel?.text = currentPropertie?.completeName

    // cell.checkMarkButton  <--- the button i need to show when the user selects the cell

    cell.containerForCell.layer.cornerRadius = CGFloat(1)
    
    return cell
}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    let selectedCell = properties.Result?[indexPath.row]
    
    print(selectedCell!)
}


func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
    return CGSize(width: 185, height: 50)
}

一个简单的方法是在 Result 数组中的对象中添加一个 属性 – 顺便将其命名为小写并声明它 non-optional.

var isSelected = false

在加载数据之后调用 reloadData() 之前将第一项的 属性 设置为 true

result.first?.isSelected = true

cellForItemAt 中显示或隐藏按钮取决于 isSelected

if currentPropertie.isSelected {
  // show cell.checkMarkButton  
} else {
  // hide cell.checkMarkButton  
}

didSelectItemAt 中切换(或设置)属性 并重新加载行

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    properties.result[indexPath.row].isSelected.toggle()
    collectionView.reloadItems(at: [indexPath] )
}

定义一个变量来存储当前选择的 IndexPath。

var selected = IndexPath(item: 0, section: 0)

更新您的以下 collectionView 委托。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dashboardCollectionViewCell", for: indexPath) as? dashboardCollectionViewCell else { return UICollectionViewCell() }
        
     let currentPropertie = properties.Result?[indexPath.row]
        
     cell.nameSurnameLabel?.text = currentPropertie?.completeName

     // Hide or unhide button
     cell.checkMarkButton.isHidden = !(indexPath == selected)
        
     cell.containerForCell.layer.cornerRadius = CGFloat(1)
        
     return cell

}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
     let previous = selected
     selected = indexPath
        
     collectionView.reloadItems(at: [previous, selected])
}