UICollectionViewCell Segue 仅在多项选择时触发
UICollectionViewCell Segue Triggered Only on Multiple Selection
几天来我一直在处理这个问题。我正在尝试从 UICollectionViewCell 转到 ViewController。虽然我了解如何执行此操作,但只能通过 selecting 多个单元格(即两次或更多次点击,但必须包含不同的单元格并且不能在 CollectionView 之外)触发 segue。
我如何更改我的代码以确保只对一个单元格进行转场而不需要一次 select 多个单元格(即正常转场,点击单元格进行转场)。
到目前为止我做了什么:
设置从 UICollectionViewCell 到 ViewController 的 Show segue。
实现了 CollectionView 方法 didSelectItemAtIndexPath 和 prepareForSegue 以将数据传递到目的地。
每隔 object 禁用多个部分,以包括 CollectionView、Cell 和 Cell 中的项目。
确保在上述所有字段上 "User Interaction Enabled"。
Cell 和 Push segue 有适当的重用 ID。
我收藏的代码ViewController如下:
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
几天来我一直在处理这个问题。我正在尝试从 UICollectionViewCell 转到 ViewController。虽然我了解如何执行此操作,但只能通过 selecting 多个单元格(即两次或更多次点击,但必须包含不同的单元格并且不能在 CollectionView 之外)触发 segue。
我如何更改我的代码以确保只对一个单元格进行转场而不需要一次 select 多个单元格(即正常转场,点击单元格进行转场)。
到目前为止我做了什么:
我收藏的代码ViewController如下:
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