Snarf
Snarf

Reputation: 15

UICollectionViewCell Segue Triggered Only on Multiple Selection

I've been working on this issue for the better part of a few days. I am attempting to segue from a UICollectionViewCell to a ViewController. While I understand how to do this, the segue is only triggered by selecting multiple cells (i.e. two or more taps, but must include different cells and not be outside of the CollectionView).

How can I alter my code to ensure that the segue happens only for one cell without the need to select more than one cell at a time (i.e. a normal segue, tap on Cell to segue).

What I've done so far:

  • Set up a Show segue from the UICollectionViewCell to the ViewController.
  • Implemented the CollectionView method didSelectItemAtIndexPath and prepareForSegue to pass data to the destination.
  • Disabled multiple section every object, to include the CollectionView, the Cell, and the items in the Cell.
  • Ensured that "User Interaction Enabled" on all the aforementioned fields.
  • Cell and Push segue have appropriate reuse ID.
  • Storyboard showing Show segue from cell to ViewController

    My code for my CollectionViewController is as follows:

    let reuseID = "HighscoreReuser"
    
    @IBOutlet var addressBar: UISearchBar!
    
    var noUsernameErrorMessage: UIAlertController = UIAlertController()
    var pageLoadErrorMessage: UIAlertController = UIAlertController()
    var noUsernameCount: Int = 0
    
    var currentPlayer: Player = Player()
    var titles = [String]()
    var levelArray: [Int] = [Int]()
    let sectionInsets = UIEdgeInsets(top: 20.0, left: 0.0, bottom: 0.0, right: 0.0)
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        var tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "DismissKeyboard")
        self.collectionView?.addGestureRecognizer(tap)
    
        titles = ["Lots of strings be here."]
    
        addressBar = UISearchBar(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width - 30, 20))
        addressBar.delegate = self
        addressBar.showsScopeBar = true
        var leftNavBarButton = UIBarButtonItem(customView: addressBar)
        self.navigationItem.leftBarButtonItem = leftNavBarButton
    
        var cellHeight = 85.0
        var cellWidth = UIScreen.mainScreen().bounds.width / 3
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 23
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseID, forIndexPath: indexPath) as CollectionViewCell
        cell.levelLabel.text = self.titles[indexPath.row]
        let curr = indexPath.row + 1
        println(curr)
        let imgName = "skill\(curr).png"
        cell.skillPicture.image = UIImage(named: imgName)
    
        cell.frame.size.height = 85.0
        cell.frame.size.width = UIScreen.mainScreen().bounds.width / 3
    
        return cell
    }
    
    func collectionView(collectionView: UICollectionView!,
        layout collectionViewLayout: UICollectionViewLayout!,
        sizeForItemAtIndexPath indexPath: NSIndexPath!) -> CGSize {
            return CGSize(width: UIScreen.mainScreen().bounds.width / 3, height: 85.0)
    }
    
    func collectionView(collectionView: UICollectionView!,
        layout collectionViewLayout: UICollectionViewLayout!,
        insetForSectionAtIndex section: Int) -> UIEdgeInsets {
            return sectionInsets
    
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 0
    }
    
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        //not needed if placed in storyboard
        //self.performSegueWithIdentifier("PushToCalc", sender: indexPath)
        println("tapped")
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "PushToCalc" {
            let indexPaths: NSArray = self.collectionView!.indexPathsForSelectedItems()
            let indexPath: NSIndexPath = indexPaths[0] as NSIndexPath
            println(indexPath)
            let destination = segue.destinationViewController as UIViewController
            destination.navigationItem.title = String(indexPath.item)
    
        }
    }
    

    .... and my CollectionViewCell, if it matters:

    import Foundation
    import UIKit
    
    class CollectionViewCell: UICollectionViewCell {
        @IBOutlet weak var skillPicture: UIImageView!
        @IBOutlet weak var levelLabel: UILabel!
    
    
    }
    

    Upvotes: 0

    Views: 705

    Answers (1)

    Jawwad
    Jawwad

    Reputation: 1336

    I think you have to set cancelsTouchesInView to false to let the gesture recognizer allow your touch to pass through.

    So under your var tap:UITapGestureRecognizer declaration try adding

    tap.cancelsTouchesInView = false
    

    Upvotes: 1

    Related Questions