繁体   English   中英

在swift4中取消选择选中标记进入Collectionview

[英]unselect checkmarks into Collectionview in swift4

我正在主视图控制器中显示带有多个复选标记的并置视图,其工作正常

在此处输入图片说明

选择选中标记后,数据显示到下一个视图控制器中。 它也正在工作。 当我回到主视图控制器时,我选择的复选标记未取消选中。 如何取消选中的选中标记。

我使用了这个参考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

    }

}

为了使您的单元格与实际选择状态同步,您需要存储它。 理想情况下,你需要model在这里,其中将包含状态(如isSelected或东西)。 但是在列出的代码中,足够存储所选单元格的[IndexPath] Set (或Array ,实际上并不重要)。

用户选择/取消选择某些单元格后,您应该更新集合(追加/删除适当的IndexPath )。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell方法中,您应该更新选中标记的状态。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM