簡體   English   中英

獲取 UICollectionView 中選定的單元格計數

[英]Get the selected cells count in 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 

您可以使用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 function 將不再被調用。 另一方面,如果您單擊已選擇的單元格,則會調用didDeselectItemAt並且您將被調用,並且您可以再次 select + 1 個單元格,因為所選項目的數量將減少 1。另請參閱調試日志在示例中。

更好的方法是基於model

在您的數據 model 添加一個屬性

var isSelected = false

並在取消/選擇單元格時相應地重新/設置它。

然后就可以簡單的統計選中的數據源項dataSource代表數據源數組

func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
    return dataSource.filter{$0.isSelected}.count < 3
}

此外,在此方法中無需取消選擇一行,因為您正在阻止用戶選擇超過 2 行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM