Ios 为什么不能使用列表类型的参数(UICollectionView)调用“indexPathForCell”?
我用这个创建了一个UICollectionView。 然后我想像下面这样将indexPath从这个UICollectionView传递到我的GameViewController 但这不起作用-即使我使用LevelCell而不是UICollectionViewCell。我得到这个错误: 无法使用“UICollectionViewCell”类型的参数列表调用“indexPathForCell” 如何获取我在UICollectionView中刚刚接触的元素的indexPath,以便将其从该函数绕过到GameViewController?您正在将发送者强制转换到UICollectionView,但它可能是UICollectionViewCellIos 为什么不能使用列表类型的参数(UICollectionView)调用“indexPathForCell”?,ios,swift,uicollectionview,viewcontroller,Ios,Swift,Uicollectionview,Viewcontroller,我用这个创建了一个UICollectionView。 然后我想像下面这样将indexPath从这个UICollectionView传递到我的GameViewController 但这不起作用-即使我使用LevelCell而不是UICollectionViewCell。我得到这个错误: 无法使用“UICollectionViewCell”类型的参数列表调用“indexPathForCell” 如何获取我在UICollectionView中刚刚接触的元素的indexPath,以便将其从该函数绕过到G
我更新了我的问题。我使用的是UICollectionViewCell或LevelCell,它也是一个单元格。但我仍然得到这个错误,不知道为什么。错误必须在其他地方。我必须是类型转换或可选/非可选问题。collectionView是一个未包装的可选实例变量吗?我没有在任何地方定义collectionView-我只是在所有答案中找到了它。在准备过程中,需要在某个地方声明变量collectionView,它必须是对UICollectionView实例的引用
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: LevelCell = collectionView.dequeueReusableCellWithReuseIdentifier("Level", forIndexPath: indexPath) as! LevelCell
cell.imgCell.image = UIImage(named: levels[indexPath.row + 1]!.levelImage)
cell.lblCell.text = levels[indexPath.row + 1]!.levelLabel
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "StartGame" {
if let gameView = segue.destinationViewController as? GameViewController {
if let cell = sender as? UICollectionViewCell, indexPath = collectionView.indexPathForCell(cell) {
gameView.level = indexPath
}
}
}
}
if let cell = sender as? UICollectionViewCell, indexPath = collectionView.indexPathForCell(cell) {
gameView.level = indexPath
}