I am showing collocation view with multiple checkmarks in main view controller.its working fine
after select checkmarks data showing into next view controller. it is also working. when i back to main view controller which I selected check marks are not unchecked. how to uncheck selected checkmarks.
I used this reference https://github.com/maximbilan/CheckMarkView
@IBOutlet weak var checkMarkViews: CheckMarkView!
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)as! CustomCollectionViewCell
let row = indexPath.row
cell.CoutnryImages.downloadImageFrom(link: imagesarray[row] , contentMode: .scaleToFill)
cell.countrynmae.text = itemsarray[row]
cell.checkMarkViews.setNeedsDisplay()
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = CollectionView.cellForItem(at: indexPath) as! CustomCollectionViewCell
var indexpathvalues = indexPath.row
cell.checkMarkViews.checked = !cell.checkMarkViews.checked
if cell.checkMarkViews.checked == true
{
PassedID.append(idarray[indexpathvalues])
_selectedcell.add(indexpathvalues)
}
else
{
if PassedID.isEmpty
{
print("no Item found ")
}
else
{
_selectedcell.remove(indexpathvalues)
PassedID.removeLast()
}
}
}
@IBAction func Addbutton(_ sender: UIBarButtonItem) {
performSegue(withIdentifier: "next", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as? nextViewController
vc?.arrayvalues = self.PassedID
_ = navigationController?.popViewController(animated: true)
}
how to uncheck the multiple selection after back to main view controller
override func viewWillAppear(_ animated: Bool) {
// get visible cells and un check them
let visibleCell = cc.visibleCells as! [CustomCollectionViewCell]
for cell in visibleCell
{
// un check here
}
}
In order to synchronize your cells with actual selection state, you need to store it. Ideally, you will need model
here, which will contain state (like isSelected
or something). But in the listed code would be just enough store Set
(or Array
, doesn't actually matter) of selected cells' [IndexPath]
.
Once a user selects/deselects some cell you should update your collection (append/remove appropriate IndexPath
).
In func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
method you should update your checkmarks' state.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.