UICollectionViewCell Segue仅在多个选择时触发

时间:2015-03-27 15:23:26

标签: swift uicollectionview segue uicollectionviewcell

我几天来一直在研究这个问题。我试图从UICollectionViewCell转到ViewController。虽然我知道如何做到这一点,但仅通过选择多个单元格(即必须包含两个或更多个单元,但必须包含不同的单元格而不是在CollectionView之外)来触发segue。

如何更改我的代码以确保segue仅针对一个单元格发生,而无需一次选择多个单元格(即正常的segue,点击Cell to segue)。

到目前为止我做了什么:

  • 设置从UICollectionViewCell到ViewController的Show segue。
  • 实现了CollectionView方法didSelectItemAtIndexPath和prepareForSegue将数据传递到目标。
  • 禁用每个对象的多个部分,以包括CollectionView,Cell和Cell中的项目。
  • 确保在所有上述字段中启用“用户交互”。
  • Cell和Push segue具有适当的重用ID。
  • Storyboard showing Show segue from cell to ViewController

    我的CollectionViewController的代码如下:

    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)
    
        }
    }
    

    ....和我的CollectionViewCell,如果重要的话:

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

    1 个答案:

    答案 0 :(得分:1)

    我认为您必须将cancelsTouchesInView设置为false才能让手势识别器允许您的触摸通过。

    因此,根据您的var tap:UITapGestureRecognizer声明,请尝试添加

    tap.cancelsTouchesInView = false