【问题标题】:UICollectionViewCell Segue Triggered Only on Multiple SelectionUICollectionViewCell Segue 仅在多选时触发
【发布时间】:2015-03-27 15:23:25
【问题描述】:

这几天我一直在研究这个问题。我试图从 UICollectionViewCell 到 ViewController。虽然我了解如何执行此操作,但只能通过选择多个单元格(即两次或多次点击,但必须包含不同的单元格并且不在 CollectionView 之外)触发 segue。

如何更改我的代码以确保只对一个单元格进行转场,而无需一次选择多个单元格(即正常转场,点击单元格进行转场)。

到目前为止我做了什么:

  • 设置从 UICollectionViewCell 到 ViewController 的 Show segue。
  • 实现了 CollectionView 方法 didSelectItemAtIndexPath 和 prepareForSegue 以将数据传递到目标。
  • 禁用每个对象的多个部分,以包括 CollectionView、Cell 和 Cell 中的项目。
  • 确保在所有上述字段上都启用了“用户交互”。
  • Cell 和 Push segue 具有适当的重用 ID。
  • 我的 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!
    
    
    }
    

    【问题讨论】:

      标签: swift uicollectionview segue uicollectionviewcell


      【解决方案1】:

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

      所以在你的var tap:UITapGestureRecognizer 声明下尝试添加

      tap.cancelsTouchesInView = false
      

      【讨论】:

      • 完美无瑕。对此感激不尽。原始点击是否设置为“DismissKeyboard”,正在朝着该功能工作,因此只剩下多个选择活动 didSelectItemAtIndexPath?
      猜你喜欢
      • 2012-12-09
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      相关资源
      最近更新 更多