Reputation: 15
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:
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
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