这个问题可能有一个非常简单的答案,但我无法理解它。我也找不到可以在网上工作的特别有用的答案。
我根本无法从UICollectionViewCell获取segue。我可能从错误的角度来看它,因为我认为它的工作方式非常类似于表视图segue。在这个前提下工作,如果我使用表格视图,它看起来像这样:
if segue.identifier == "TripsToPastTripDetailsSegue" {
let newViewController = segue.destinationViewController as! pastTripDetailViewController
let selectedRow: NSManagedObject = tripsList[tableView.indexPathForSelectedRow!.row] as NSManagedObject
newViewController.passedTrip = selectedRow as! Trips
}
因此,使用表视图,我将一个对象(从Core Data)传递给另一个视图控制器。简单。但我无法将其转化为集合视图。
我一直在尝试这些方面:
if segue.identifier == "pastTripCollectionToSavedLocationSegue" {
let newViewController = segue.destinationViewController as! pastTripDetailViewController
let selectedRow: NSManagedObject = locationsList[collectionView.indexPathForCell(pastLocationImageCollectionViewCell)] as! NSManagedObject
newViewController.passedTrip = selectedRow as! Trips
}
但这给了我一个错误找不到成员'indexPathForCell',所以我显然做了一些非常错误的事情。这是我在玩代码时遇到的许多错误之一。
有人能指出我正确的方向吗?
编辑:
根据@Laffen的要求,UICollectionView出现在代码中的以下位置:
class pastTripDetailViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, MKMapViewDelegate {
作为IBOutlet:
@IBOutlet weak var collectionView: UICollectionView!
ViewDidLoad中的数据源和委托:
self.collectionView.dataSource = self
self.collectionView.delegate = self
以下是其余部分:
func collectionView(collectionView: UICollectionView, numberOfSectionsInCollectionView indexPath: Int) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.locationsList.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let location = self.locationsList[indexPath.row]
let cell: pastLocationImageCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("imageCell", forIndexPath: indexPath) as! pastLocationImageCollectionViewCell
cell.locationImage.image = UIImage(data: location.image!)
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("pastTripCollectionToSavedLocationSegue", sender: self)
}
答案 0 :(得分:5)
也许这对你有用:
let indexPath: NSIndexPath = self.collectionView.indexPathsForSelectedItems().first!
let selectedRow: NSManagedObject = locationsList[indexPath] as! NSManagedObject
这仅在locationsList
类型为字典并且初始化如下时才有效:
var locationsList = [NSIndexPath: NSManagedObject]()
希望这是有帮助的
编辑:
您似乎试图传递pastLocationImageCollectionViewCell
中的indexPathForCell
类型。参数必须是pastLocationImageCollectionViewCell
类型的实例。
试试这个:
已编辑:以下示例仅在UICollectionView中只有一个部分时才能正常工作。
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "pastTripCollectionToSavedLocationSegue" {
let newViewController = segue.destinationViewController as! pastTripDetailViewController
let indexPath = sender as! NSIndexPath
let selectedRow: NSManagedObject = locationsList[indexPath.row] as! NSManagedObject
newViewController.passedTrip = selectedRow as! Trips
}
}
// Set the indexPath of the selected item as the sender for the segue
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("pastTripCollectionToSavedLocationSegue", sender: indexPath)
}
答案 1 :(得分:2)
试一试。
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("yourSegueIdentifier", sender: self)
}
答案 2 :(得分:0)
试试这段代码,把它放在collectionview的类中(Swift 4,xcode 9)
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("cell clicked")
let singleDetail = storyboard?.instantiateViewController(withIdentifier: "urDestinationViewControllerIdentifier") as? urDestinationViewControllerClass
singleDetail?.modalPresentationStyle = .fullScreen
self.present(singleDetail!, animated: true) {() -> Void in }
}