JamieS
JamieS

Reputation: 31

UICollectionView cell needs to be tapped twice for segue to happen

I know this will be something simple; however, I have created a UICollectionView that displays just some colours for now. When a cell is tapped I want it to perform a segue with the cell number that was tapped.

At the moment, if I tap a cell nothing happens. If I touch the same cell again nothing happens, but if I touch any other cell the segue occurs successfully and the cell clicked gets passed on, albeit with the first cells touched number.

It is in a navigation stack. Here is my code:

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

Any pointers appreciated.

Upvotes: 0

Views: 469

Answers (1)

f_qi
f_qi

Reputation: 699

Your issue is with this line didDeselectItemAt, make sure you use didSelectItemAt.

didDeselectItemAt is the reason why you have to tap twice for segue, you select the cell then deselect the cell, once the cell is deselected, your code perform segue.

Upvotes: 1

Related Questions