需要点击两次UICollectionView单元才能发生segue

时间:2017-06-01 15:34:15

标签: uicollectionview

我知道这会很简单;但是,我创建了一个UICollectionView,它现在只显示一些颜色。当一个单元被轻敲时,我希望它用一个被轻敲的单元号执行一个segue。

目前,如果我点击一个单元格没有任何反应。如果我再次触摸同一个单元格没有任何反应,但是如果我触摸任何其他单元格,则segue会成功发生并且单击的单元格会被传递,尽管第一个单元格会触及数字。

它位于导航堆栈中。这是我的代码:

//Everything Collection View


//header text
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "SectionHeader", for: indexPath as IndexPath) as! SectionHeaderPractice
    header.headerLabel.text = "SELECT A TOPIC..."
    return header
}

//number of cells
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
}

//height and width of cell
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let cellWidth = (view.frame.width/2)-20
    let cellHeight = view.frame.height/4
    return CGSize(width: cellWidth, height: cellHeight)
}

//cell content

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let content = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as UICollectionViewCell

    //set text
    let desc = content.viewWithTag(3) as! UILabel
    desc.text = shieldTextArray[(indexPath as NSIndexPath).row]

    //set color scale
    let score = scoreArray[(indexPath as NSIndexPath).row]
    let redC = 2*score
    let greenC = 2*(1-score)
    content.backgroundColor = UIColor.init(red: CGFloat(redC), green: CGFloat(greenC), blue: 0.45, alpha: 1)

    return content
}

override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    //sets variable to clicked cell
    //cellClicked = indexPath.row

    //changes background colour on clicking
    //let content1 = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as UICollectionViewCell
    //content1.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 1)

    //Performs segue
    //print(cellClicked)
    performSegue(withIdentifier: "practiceSegue", sender: self)

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsetsMake(10,10,10,10)
}

//End Everything Collection View

任何指示赞赏。

1 个答案:

答案 0 :(得分:0)

您的问题在于didDeselectItemAt这一行,请务必使用didSelectItemAt

didDeselectItemAt之所以你必须为segue点击两次,你选择单元格然后取消选择单元格,一旦取消选择单元格,你的代码就会执行segue。

相关问题