【问题标题】:UICollectionView cell needs to be tapped twice for segue to happenUICollectionView 单元需要被点击两次才能发生 segue
【发布时间】:2017-06-01 15:34:14
【问题描述】:

我知道这很简单;但是,我创建了一个 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

任何指针表示赞赏。

【问题讨论】:

    标签: uicollectionview


    【解决方案1】:

    您的问题在于didDeselectItemAt 这一行,请确保使用didSelectItemAt

    didDeselectItemAt 是您必须点击两次进行转场的原因,选择单元格然后取消选择单元格,一旦取消选择单元格,您的代码就会执行转场。

    【讨论】:

    • 非常感谢...不敢相信我错过了!
    猜你喜欢
    • 2015-12-24
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多