集合视图上的多项选择

时间:2019-03-20 11:06:20

标签: swift xcode uicollectionviewcell

一次选择和取消选择单元格时出现问题,它可以工作。但是,如果我再次选择相同的单元格,则什么也没有发生,它不会触发didselect函数。我还启用了多项选择。谢谢您的帮助。

我的CollectionViewCell代码:

class EventItemCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var txtLabel: UILabel!
@IBOutlet weak var imageCheck: UIImageView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

public func toggleSelected() {

    if (isSelected == false) {

        //Hide check mark image.
        self.imageCheck.image = UIImage(named: "success-1")
        isSelected = true
    }else{

        //Show check mark image.
        self.imageCheck.image = UIImage(named: "success-2")
        isSelected = false
    }
}

}

我的视图控制器代码:

import UIKit

class EventItemSelectionViewController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!

    var items: [Item] = [Item(imageName: "vegetables"), Item(imageName: "cheers"), Item(imageName: "cocktail"), Item(imageName: "ice-cream"), Item(imageName: "soup"), Item(imageName: "steak")]

    var itemsNames = ["Salades", "Boisson alcoolisée", "Boisson non-alcoolisée", "Dessert", "Entrée", "Viande"]
    var itemsCheck = [UIImage(named: "success-2"), UIImage(named: "")]

    var collectionViewFlowLayout: UICollectionViewFlowLayout!
    let cellIdentifier = "ItemCollectionViewCell"


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.edgesForExtendedLayout = UIRectEdge.bottom
        setupCollectionView()
        collectionView.allowsMultipleSelection = true
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        setupCollectionViewItemSize()
    }

    private func setupCollectionView(){
        collectionView.delegate = self
        collectionView.dataSource = self

        let nib = UINib(nibName: "EventItemCollectionViewCell", bundle: nil)
        collectionView.register(nib, forCellWithReuseIdentifier: cellIdentifier)
    }

    private func setupCollectionViewItemSize(){
        if collectionViewFlowLayout == nil {
            let numberOfItemPerRow: CGFloat = 2
            let lineSpacing: CGFloat = 1
            let interItemSpacing: CGFloat = 1

            let width = (collectionView.frame.width - (numberOfItemPerRow - 1) * interItemSpacing) / numberOfItemPerRow
            let height = width

            collectionViewFlowLayout = UICollectionViewFlowLayout()
            collectionViewFlowLayout.itemSize = CGSize(width: width, height: height)
            collectionViewFlowLayout.sectionInset = UIEdgeInsets.zero
            collectionViewFlowLayout.scrollDirection = .vertical
            collectionViewFlowLayout.minimumLineSpacing = lineSpacing
            collectionViewFlowLayout.minimumInteritemSpacing = interItemSpacing

            collectionView.setCollectionViewLayout(collectionViewFlowLayout, animated: true)
        }

    }

}

extension EventItemSelectionViewController: UICollectionViewDataSource, UICollectionViewDelegate {



    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemCollectionViewCell", for: indexPath) as! EventItemCollectionViewCell

        cell.imageView.image = UIImage(named: items[indexPath.item].imageName)
        cell.txtLabel.text = itemsNames[indexPath.row]

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("\(indexPath)")

        let cell = collectionView.cellForItem(at: indexPath) as? EventItemCollectionViewCell
        cell?.isSelected = true
        cell?.toggleSelected()
    }

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {

        let cell = collectionView.cellForItem(at: indexPath) as? EventItemCollectionViewCell
        cell?.isSelected = false
        cell?.toggleSelected()
    }

}

2 个答案:

答案 0 :(得分:0)

  

但是,如果我再次选择相同的单元格,则什么也不会发生

因为您总是在json.put("programArgsList", Arrays.asList("--bootstrap.server", "localhost:9092", "--zookeeper.server", "localhost:2181", "--query", "where agentHost='192.168.170.111'", "--source.topic", "demo1", ...)); 内将其设置为true

didSelectItemAt

cell?.isSelected = true
cell?.toggleSelected()

答案 1 :(得分:0)

选中这个

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

           let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemCollectionViewCell", for: indexPath) as! EventItemCollectionViewCell
 {
            if !itemsNames.contains(indexPath.item) {
                cell.backgroundColor = .red
                self.itemsNames.append(indexPath.row)
            } else {
                cell.backgroundColor = .white
                self.itemsNames.remove(object: indexPath.item)
            }
        }
    }