Ios UICollectionView的单元格选择在swift中不起作用

Ios UICollectionView的单元格选择在swift中不起作用,ios,swift,uicollectionviewcell,Ios,Swift,Uicollectionviewcell,我在这里编写了示例代码-当我点击“选择”按钮时,它应该选择UICollectionView的所有单元格。但is不起作用,且未报告任何错误。下面是代码 //collectionView是UICollectionView的名称 let collectonViewCount = collectionView.numberOfItemsInSection(0); for i in 0...collectonViewCount { let indexPath =

我在这里编写了示例代码-当我点击“选择”按钮时,它应该选择UICollectionView的所有单元格。但is不起作用,且未报告任何错误。下面是代码

//collectionView是UICollectionView的名称

let collectonViewCount = collectionView.numberOfItemsInSection(0);

        for i in 0...collectonViewCount {

            let indexPath = NSIndexPath(forRow: i, inSection: 0)
            collectionView.selectItemAtIndexPath(indexPath, animated: true, scrollPosition: .None)

        }
我不确定上面的代码可能有什么错误。

CyberGeeek

你可以试试

for i in 0...collectonViewCount {
let indexPath = NSIndexPath(forRow: i, inSection: 0)
collectionView.delegate?.collectionView!(collectionView, didSelectItemAtIndexPath: indexPath)
}

当然,这个答案假设您已经设置了collectionView委托并实现了
didSelectItemAtIndexPath
这将选择collectionView单元格并触发didSelectItemAtIndexPath:)

不起作用。。。它正在崩溃。。在…内didSelectItemAtIndexPath@cybergeeeek:我可以知道坠机情况吗???我从来没有对这段代码有过任何问题,事实上,我也在一个公认的应答器中使用过它(虽然不是同一个问题):)错误是“致命错误:在打开可选值时意外发现零”@CyberGeeek:您是否设置了collectionView.delegate=self????我已经在我的回答中明确指出,只有当您指派了代表时,我才能工作:)@cybergeeek:您可以发布修改后的代码吗?我随身携带一个工作代码,它的工作绝对正常