从 Swift 中的 UICollectionView 继续
Segue from a UICollectionView in Swift
这个问题可能有一个非常简单的答案,但我无法理解它。我也找不到适用于任何在线地方的特别有用的答案。
我根本无法从 UICollectionViewCell 中获取 segue。我可能从错误的角度来看它,因为我认为它的工作方式与 table 视图 segue 非常相似。在那个前提下工作,如果我使用 table 视图,它看起来像这样:
if segue.identifier == "TripsToPastTripDetailsSegue" {
let newViewController = segue.destinationViewController as! pastTripDetailViewController
let selectedRow: NSManagedObject = tripsList[tableView.indexPathForSelectedRow!.row] as NSManagedObject
newViewController.passedTrip = selectedRow as! Trips
}
因此,对于 table 视图,我会将单个对象(来自 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)
}
也许这对你有用:
let indexPath: NSIndexPath = self.collectionView.indexPathsForSelectedItems().first!
let selectedRow: NSManagedObject = locationsList[indexPath] as! NSManagedObject
这仅在 locationsList
是字典类型并且初始化如下时有效:
var locationsList = [NSIndexPath: NSManagedObject]()
希望对您有所帮助
编辑:
您似乎正试图在 indexPathForCell
中传递 pastLocationImageCollectionViewCell
类型。参数必须是 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)
}
试试吧。
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("yourSegueIdentifier", sender: self)
}
试试这个代码,把它放在集合视图的class中(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 }
}
这个问题可能有一个非常简单的答案,但我无法理解它。我也找不到适用于任何在线地方的特别有用的答案。
我根本无法从 UICollectionViewCell 中获取 segue。我可能从错误的角度来看它,因为我认为它的工作方式与 table 视图 segue 非常相似。在那个前提下工作,如果我使用 table 视图,它看起来像这样:
if segue.identifier == "TripsToPastTripDetailsSegue" {
let newViewController = segue.destinationViewController as! pastTripDetailViewController
let selectedRow: NSManagedObject = tripsList[tableView.indexPathForSelectedRow!.row] as NSManagedObject
newViewController.passedTrip = selectedRow as! Trips
}
因此,对于 table 视图,我会将单个对象(来自 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)
}
也许这对你有用:
let indexPath: NSIndexPath = self.collectionView.indexPathsForSelectedItems().first!
let selectedRow: NSManagedObject = locationsList[indexPath] as! NSManagedObject
这仅在 locationsList
是字典类型并且初始化如下时有效:
var locationsList = [NSIndexPath: NSManagedObject]()
希望对您有所帮助
编辑:
您似乎正试图在 indexPathForCell
中传递 pastLocationImageCollectionViewCell
类型。参数必须是 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)
}
试试吧。
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("yourSegueIdentifier", sender: self)
}
试试这个代码,把它放在集合视图的class中(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 }
}