提问者:小点点

在UICollectionView中获取所选单元格计数


如何从UICollectionView中获取所选单元格计数和所选单元格数组,我需要设置选择的限制。下面是我的代码,它不工作。请引导。谢谢

 func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
    let indexPaths = collectionView.indexPathsForSelectedItems!
        print(indexPaths)
    if let selectedItems = collectionView.indexPathsForSelectedItems {
                if selectedItems.count >= 3 {
                    collectionView.deselectItem(at: indexPath as IndexPath, animated: true)
                    return false
                }
            }
    return true
}

上面的代码,当编写在didSelect方法中时,只返回选定的单元格索引,但这里只跳到最后一行

return true 

共1个答案

匿名用户

您可以使用UICollectionViewDelegate中的ShouldSelectItemat委托方法来实现:

func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
    return collectionView.indexPathsForSelectedItems?.count ?? 0 <=  selectionLimit
}

演示其工作方式的示例演示:

class ViewController : UIViewController {
    private var myCollectionView:UICollectionView?
    private var selectionLimit = 4

    override func viewDidLoad() {
        super.viewDidLoad()

        let view = UIView()
        view.backgroundColor = .white

        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: UIScreen.main.bounds.width * 0.7, height: 60)

        myCollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        myCollectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
        myCollectionView?.backgroundColor = UIColor.white

        myCollectionView?.allowsMultipleSelection = true
        myCollectionView?.dataSource = self
        myCollectionView?.delegate = self

        view.addSubview(myCollectionView ?? UICollectionView())

        self.view = view
    }
}
extension ViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 20
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath)
        myCell.backgroundColor = UIColor.blue
        return myCell
    }
}
extension ViewController: UICollectionViewDelegate {

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("Did select a cell at \(indexPath.row)")
        let itemsLeftToSelect = selectionLimit - (collectionView.indexPathsForSelectedItems?.count ?? 0)
        print("Have \(itemsLeftToSelect) items to select")
    }

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        print("Did deselect a cell at \(indexPath.row)")
        let itemsLeftToSelect = selectionLimit - (collectionView.indexPathsForSelectedItems?.count ?? 0)
        print("Have \(itemsLeftToSelect) items to select")
    }

    func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
        return collectionView.indexPathsForSelectedItems?.count ?? 0 <  selectionLimit
    }
}

当您达到选择限制时,ShouldSelectItemat将返回false,DidSelectItemat函数将不再被调用。另一方面,如果您单击已选择的单元格,则DidDeselectItemat将被调用,您也将被调用,并且您能够再次选择+1个单元格,因为所选项的数量将随着1而减少。另请参阅示例中的调试日志。