ios - 在具有 Storyboard segue 的 collectionviewcell super View 上带有推送 Segue 的按钮会引发错误

标签 ios swift uicollectionview segue

我是 swift 的新手,我遇到了这个错误。我有一个 viewController,它有一个 Collection View ,其中有 Storyboard segue 到 viewContoller 和两个按钮,这些按钮将 segue 推送到其他 viewController。当我尝试单击这些按钮时,出现 fatal error ,表明发送者不是具有以下代码的 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中,不要使用断言,而是使用可选链接来转换发送者变量:

if let cell = sender as? UICollectionViewCell 

而不是:

assert(sender as? UICollectionViewCell != nil, "sender is not a collection view")

关于ios - 在具有 Storyboard segue 的 collectionviewcell super View 上带有推送 Segue 的按钮会引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33869138/

相关文章:

android - 如何对蓝牙配件进行单元测试?

ios - 通过按钮操作进行 segue

ios - 如何使 UICollectionView 单元格可移动?

ios - 键盘出现后布局被破坏

ios - touchesBegan 未在 UICollectionViewController 中触发

ios - Swift 3 iOS 10 - 带有图像和文本的 UICollectionView 无法正确显示

iPhone:根据位置排序

ios - 如何在 Swift 中编写通用的 apply() 函数?

swift - api返回的数据总是nil?

ios - 展开标签并转换为 Int()