Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何限制UICollectionView中的可选单元格_Ios_Swift_Xcode_Uicollectionview - Fatal编程技术网

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

Ios 如何限制UICollectionView中的可选单元格,ios,swift,xcode,uicollectionview,Ios,Swift,Xcode,Uicollectionview,在我的应用程序中,我有一个带有12 x 4个正方形的UICollectionView。我希望用户在同一周期内只能选择其中一行中的4个。我怎样才能阻止其他行?我在UITableView中读到一些关于标题的内容,但在UICollectionView实现下面提到的UICollectionViewDelegate func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath)

在我的应用程序中,我有一个带有12 x 4个正方形的
UICollectionView
。我希望用户在同一周期内只能选择其中一行中的4个。我怎样才能阻止其他行?我在
UITableView
中读到一些关于标题的内容,但在
UICollectionView
实现下面提到的
UICollectionViewDelegate

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

return collectionView.indexPathsForSelectedItems?.count定义以下变量:

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")
        }
    }
}
尝试以下解决方案:

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

let numerOfItems = 20
var firstIndex=-1
var lastIndex=-1
var arrayselectedIndex=[Int]()
var setEligibleIndexForSelection=Set()
设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)
            }
        }
    }
}
扩展视图控制器:UICollectionViewDelegate{ func collectionView(collectionView:UICollectionView,shouldSelectItemAt indexPath:indexPath)->Bool{ 开关(self.setEligibleIndexForSelection.contains(indexath.row),self.arraySelectedIndexes.count==0){ 案例(假,假): 返回错误 违约: 返回真值 } } func collectionView(collectionView:UICollectionView,didSelectItemAt indexPath:indexPath){ self.arrayselectedIndex.append(indexPath.row) self.arrayselectedIndex.sort() 让cell=collectionView.cellForItem(位于:indexPath) 单元格?.backgroundColor=UIColor.lightGray self.firstIndex=self.arrayselectedindex[0] self.lastIndex=self.arrayselectedIndex[self.arrayselectedIndex.count-1] self.updateEligibleIndexArray()文件 } func collectionView(collectionView:UICollectionView,DidDecelectItemat indexPath:indexPath){ 让indexOfSelectedData=self.arraySelectedIndexes.firstIndex(of:indexath.row) self.arrayselectedindex.remove(位于:indexOfSelectedData!) 如果self.arrayselectedIndex.count>0{ self.firstIndex=self.arrayselectedindex[0] self.lastIndex=self.arrayselectedIndex[self.arrayselectedIndex.count-1] self.updateEligibleIndexArray()文件 } 否则{ self.firstIndex=-1 self.lastIndex=-1 self.setEligibleIndexForSelection.removeAll() } 让cell=collectionView.cellForItem(位于:indexPath) 单元格?.backgroundColor=UIColor.white } func updateeEligibleIndexarray(){ self.setEligibleIndexForSelection.removeAll()
对于self.firstIndex中的i..在使用collectionview的委托选择单元格之前,您可以检查您的条件。这不只是限制数量吗?例如,我想选择[0,1]-[0,4]/[0,5]-[0,8]因此,您可以更新
collectionView.indexPathsForSelectedItems?.count是的,这对我来说非常有效。非常感谢:我如何才能做到这一点,第一行是可选的,是顶部的那一行?