【问题标题】:How can I limit the selectable cells in a UICollectionView如何限制 UICollectionView 中的可选单元格
【发布时间】:2018-10-05 05:41:34
【问题描述】:

在我的应用程序中,我有一个 UICollectionView,有 12 x 4 个正方形。我希望用户在同一周期中只能连续选择 4 个。我怎样才能阻止其他行?我在UITableView 中读到了一些关于标题的内容,但这在UICollectionView 中不起作用

【问题讨论】:

标签: ios swift xcode uicollectionview


【解决方案1】:

实现下面提到的UICollectionViewDelegate

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

【讨论】:

  • 这不是限制数量吗?例如,我希望只有 [0,1] - [0,4] 是可选的 / [0,5] - [0,8] 等等
  • 你可以用你自己的逻辑更新collectionView.indexPathsForSelectedItems?.count &lt;= 4
【解决方案2】:

定义以下变量:

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")
        }
    }
}

【讨论】:

    【解决方案3】:

    试试这个解决方案:

    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)
                }
            }
        }
    }
    

    我可能错过了一些场景,所以请告诉我它是否有效。

    【讨论】:

    • 是的,这对我来说非常有用,非常感谢:D
    • 我该怎么做,第一行是可选择的,是在顶部的那一行?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多