gpt4 book ai didi

swift - UICollectionViewCell Segue 仅在多项选择时触发

转载 作者:行者123 更新时间:2023-11-30 10:17:47 24 4
gpt4 key购买 nike

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


    }

    最佳答案

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

    因此,在您的 var tap:UITapGestureRecognizer 声明下尝试添加

    tap.cancelsTouchesInView = false

    关于swift - UICollectionViewCell Segue 仅在多项选择时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29304279/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com