Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.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_Iphone_Xcode_Uicollectionview - Fatal编程技术网

Ios 在UICollectionView中获取选定的单元格计数

Ios 在UICollectionView中获取选定的单元格计数,ios,swift,iphone,xcode,uicollectionview,Ios,Swift,Iphone,Xcode,Uicollectionview,如何从UICollectionView获取所选单元格数和所选单元格数组,我需要设置选择的限制。下面是我不工作的代码。请导游。谢谢 func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool { let indexPaths = collectionView.indexPathsForSelectedItems! pr

如何从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
}
func collectionView(collectionView:UICollectionView,shouldSelectItemAt indexPath:indexPath)->Bool{
返回collectionView.indexPathsForSelectedItems?.count±0 Int{
返回20
}
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
让myCell=collectionView.dequeueReusableCell(带有reuseidentifier:“myCell”,用于:indexPath)
myCell.backgroundColor=UIColor.blue
返回迈塞尔
}
}
扩展视图控制器:UICollectionViewDelegate{
func collectionView(collectionView:UICollectionView,didSelectItemAt indexPath:indexPath){
打印(“在\(indexPath.row)处选择了一个单元格”)
让itemsLeftToSelect=selectionLimit-(collectionView.indexPathsForSelectedItems?.count±0)
打印(“Have\(itemsLeftToSelect)要选择的项目”)
}
func collectionView(collectionView:UICollectionView,DidDecelectItemat indexPath:indexPath){
打印(“在\(indexPath.row)处取消选择单元格”)
让itemsLeftToSelect=selectionLimit-(collectionView.indexPathsForSelectedItems?.count±0)
打印(“Have\(itemsLeftToSelect)要选择的项目”)
}
func collectionView(collectionView:UICollectionView,shouldSelectItemAt indexPath:indexPath)->Bool{
返回collectionView.indexPathsForSelectedItems?计数0

当达到选择限制时,
shouldSelectItemAt
将返回false,并且不再调用
didSelectItemAt
函数。另一方面,如果您单击已选择的单元格,则会调用
didDeselectItemAt,您将被调用,并且您可以再次选择+1单元格,因为所选项目的数量将随着1的减少而减少。另请参见示例中的调试日志。

更好的方法是基于模型的

在数据模型中添加属性

var isSelected = false
并在取消/选择单元格时相应地重新-/设置它

然后您可以简单地计算所选数据源项–
dataSource
表示数据源数组

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

此外,此方法中不需要取消选择行,因为您会阻止用户选择多于2行。

不工作,只打印所选单元格的行index@iPhone7您的
indexPathsForSelectedItems
数组很可能返回1,因为
allowsMultipleSelection
设置为false。尝试将其设置为true,它将正常工作。如果这样,我不应该被允许选择多个单元格,但我can@iPhone7查看我的更新。我为您提供了一个可以测试它的示例。很抱歉,这也不起作用,DIDDENSELECT从未调用过。即使在取消选择单元格时,也可以在XPath中获取选定项,但无法获取选定单元格的数组,因为单元格是用于reuse@SPatel我需要得到至少一个选定的单元格计数,只需将其索引路径存储在数组中(如果allready包含,则将其删除)@SPateli不这么认为。我们可以从集合中获取计数。IndExpathsForSelectedItem是否将
allowsMultipleSelection
设置为true?
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
    return dataSource.filter{$0.isSelected}.count < 3
}