2017-06-01 90 views
0

我知道这将是简单的;不过,我已经创建了一个UICollectionView,现在只显示一些颜色。当点击一个单元格时,我希望它用点击的单元格编号执行一个循环。UICollectionView单元需要点击两次以便继续发生

此刻,如果我点击一个单元格,什么都不会发生。如果我再次触摸同一个单元格,则不会发生任何事情,但是如果我触摸任何其他单元格,则会成功发生并单击单元格,尽管第一个单元格被触动了数字。

它在导航堆栈中。这里是我的代码:

//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 

任何指针赞赏。

回答

0

您的问题是与这条线didDeselectItemAt,请确保您使用didSelectItemAt

didDeselectItemAt这就是为什么您必须点击两次以查找segue的原因,您选择该单元格,然后取消选择该单元格,一旦该单元格被取消选中,您的代码执行segue。

+0

非常感谢你......不敢相信我错过了! – JamieS

相关问题