繁体   English   中英

从collectionviewcell快速切换到新视图会导致单击单元格时中断

[英]swift segue from collectionviewcell to a new view causes break when cell is clicked

当我在运行时单击单元格时,程序似乎中断了,我得到了: EXC_BREAKPOINT(code=EXC_I386_BPT,subcode=0x0

直到我创建了一个从collectionviewcell到新视图控制器的segue为止,我才没有这个问题。

这是我用于主视图控制器的代码,并将其发送到视图控制器:

主视图控制器:

class FlashViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

@IBOutlet var collectionView: UICollectionView!

var decks: [Deck] = []

    override func viewDidLoad() {
    super.viewDidLoad()
    // Move on ...
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 75, left: 20, bottom: 10, right: 20)
    layout.itemSize = CGSize(width: 150, height: 200)
    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    self.collectionView.dataSource = self
    self.collectionView.delegate = self
    collectionView.registerClass(DeckCollectionViewCell.self, forCellWithReuseIdentifier: "DeckCollectionViewCell")
    collectionView.backgroundColor = UIColor.whiteColor()
    self.view.addSubview(collectionView!)

    var deck1 = Deck()
    deck1.name = "SAT is the bomb"
    self.decks.append(deck1)

}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.decks.count
}


func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var deck = self.decks[indexPath.row]
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("DeckCollectionViewCell", forIndexPath: indexPath) as DeckCollectionViewCell
    cell.backgroundColor = UIColor.blackColor()
    cell.textLabel?.text = deck.name
    cell.textLabel.textColor = UIColor.whiteColor()
    cell.imageView?.image = UIImage(named: "circle")
    return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    self.performSegueWithIdentifier("decksToCardsSegue", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var nextViewController = segue.destinationViewController as NewDeckViewController
    nextViewController.deckCollection = self
}
}

新视图控制器:

class DeckViewController : UIViewController{

     override func viewDidLoad() {
         super.viewDidLoad()
         // Move on ...
     }
}

检查您的Storyboard Segue标识符是否为“ decksToCardsSegue”,检查您的标识符下的Segue和Segue类,并确保它们正确无误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM