我是新手,很快就陷入了这个错误。我有一个viewController,它有一个集合视图,该故事板具有到viewContoller的故事板序列,以及两个按钮,可以将segue推送到其他一些viewController。当我尝试单击这些按钮时,出现一个致命错误,该错误表明发件人不是具有以下代码的collectionView。
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
{
if let cell = collectionViewOne.cellForItemAtIndexPath(indexPath)
{
performSegueWithIdentifier("showDeals", sender: cell)
}
else
{
// Error indexPath is not on screen: this should never happen.
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
assert(sender as? UICollectionViewCell != nil, "sender is not a collection view")
if let indexPath = self.collectionViewOne?.indexPathForCell(sender as! UICollectionViewCell) {
if segue.identifier == "showDeals" {
let detailVC: DealsTableViewController = segue.destinationViewController as! DealsTableViewController
detailVC.someCategory = MyIds[indexPath.row]
}
} else {
// Error sender is not a cell or cell is not in collectionView.
}
}
尝试添加
print("sender type : \(sender.dynamicType)")
在prepareForSegue内部,不要使用assert,而是使用可选链来强制转换sender变量:
if let cell = sender as? UICollectionViewCell
代替 :
assert(sender as? UICollectionViewCell != nil, "sender is not a collection view")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句