swift2 从Swift中的UICollectionView开始延续

vqlkdk9b  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(225)

这个问题可能有一个非常简单的答案,但我想不明白,也找不到一个特别有用的答案,在网上任何地方都适用。
我只是不能从UICollectionViewCell中得到一个segue。我可能是从错误的Angular 来的,因为我假设它以一种非常类似于表视图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   
    }

但是这给了我一个错误Cannot find member '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)
}
50few1ms

50few1ms1#

也许这对你有用:

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)
}
gcxthw6b

gcxthw6b2#

"试试看"

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("yourSegueIdentifier", sender: self)
}
hof1towb

hof1towb3#

尝试这段代码,将其放在集合视图的类中(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 }
        }

相关问题