I have a button with a checkmark image and I want when the user selects the cell the button to show from the hide state it was.
I have already configured the collectionView and just need to make the option to show the checkmark button from the hide state to a selected state only for the cell selected.
Also if there is a possibility to let the checkmark button shown only on the first cell when the data is loaded.
Code from 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)
}
CodePudding user response:
Define a variable to store the current selected IndexPath.
var selected = IndexPath(item: 0, section: 0)
Update your following collectionView delegates.
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])
}
CodePudding user response:
A simple way is to add a property in the object which is in the Result
array – by the way name it lowercased and declare it non-optional.
var isSelected = false
after loading the data and before calling reloadData()
set the property of the first item to true
result.first?.isSelected = true
In cellForItemAt
show or hide the button depending on isSelected
if currentPropertie.isSelected {
// show cell.checkMarkButton
} else {
// hide cell.checkMarkButton
}
In didSelectItemAt
toggle (or set) the property and reload the row
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
properties.result[indexPath.row].isSelected.toggle()
collectionView.reloadItems(at: [indexPath] )
}