如何限制UICollectionView中的可选单元格

时间:2018-10-05 05:41:35

标签: ios swift xcode uicollectionview

在我的应用程序中,我有一个func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return xxx; } ,具有12 x 4的正方形。我希望用户只能在同一周期中连续选择4个。如何阻止其他行?我在UICollectionView中读到一些有关标头的信息,但这在UITableView

中不起作用

3 个答案:

答案 0 :(得分:1)

定义以下变量:

var firstSelectedIndex: Int?
var nextEligibleCount: Int = 0

然后更新collectionView委托方法:

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

    switch indexPath.row {
    case 0, 4, 8, 12:
        firstSelectedIndex = indexPath.row
        print("-----New row click has been started------")
        print(String(indexPath.row) + " is clicked")
        nextEligibleCount = 1
    default:
        checkClick(atIndex: indexPath.row)
    }
}

checkClick() 的定义如下:

func checkClick(atIndex selectedIndex: Int) {
    if firstSelectedIndex != nil {
        if selectedIndex == firstSelectedIndex! + nextEligibleCount {
            print(String(selectedIndex) + " is clicked")
            nextEligibleCount = nextEligibleCount + 1
        } else {
            print("Invalid click")
        }
    }
}

答案 1 :(得分:0)

下面提到的UICollectionViewDelegate

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

答案 2 :(得分:0)

尝试以下解决方案:

var firstIndex = -1
var lastIndex = -1
var arraySelectedIndexes = [Int]()
var setEligibleIndexesForSelection = Set<Int>()

let numerOfItems = 20

我尝试考虑所有情况:

extension ViewController : UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
        switch (self.setEligibleIndexesForSelection.contains(indexPath.row), self.arraySelectedIndexes.count == 0) {
        case (false,false):
            return false
        default:
            return true
        }
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.arraySelectedIndexes.append(indexPath.row)
        self.arraySelectedIndexes.sort()

        let cell = collectionView.cellForItem(at: indexPath)
        cell?.backgroundColor = UIColor.lightGray

        self.firstIndex = self.arraySelectedIndexes[0]
        self.lastIndex = self.arraySelectedIndexes[self.arraySelectedIndexes.count - 1]

        self.updateEligibleIndexArray()
    }

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        let indexOfSelectedData = self.arraySelectedIndexes.firstIndex(of: indexPath.row)
    self.arraySelectedIndexes.remove(at: indexOfSelectedData!)

        if self.arraySelectedIndexes.count > 0 {
            self.firstIndex = self.arraySelectedIndexes[0]
            self.lastIndex = self.arraySelectedIndexes[self.arraySelectedIndexes.count - 1]
            self.updateEligibleIndexArray()
        }
        else {
            self.firstIndex = -1
            self.lastIndex = -1
            self.setEligibleIndexesForSelection.removeAll()
        }

        let cell = collectionView.cellForItem(at: indexPath)
        cell?.backgroundColor = UIColor.white

    }

    func updateEligibleIndexArray() {
        self.setEligibleIndexesForSelection.removeAll()

        for i in self.firstIndex..<self.firstIndex+4 {
            if i < self.numerOfItems && !self.arraySelectedIndexes.contains(i) {
                self.setEligibleIndexesForSelection.insert(i)
            }
        }

        for i in self.lastIndex-3..<self.lastIndex {
            if i >= 0 && !self.arraySelectedIndexes.contains(i) {
                self.setEligibleIndexesForSelection.insert(i)
            }
        }
    }
}

我可能错过了几种情况,所以请让我知道它是否有效。