我对斯威夫特不熟悉。我试图在细节视图中显示一个图像,这个图像来自一个UICollectionViewController。我的问题是:
如何通过segue将数据发送到细节视图?我不能使用if let indexPath = collectionView.indexPathForSelectedRow...所以正确的方法是什么?
我如何判断用户何时点击UICollectionViewController的不同部分的图像,然后将此信息传递给DetailView
audiucollectionviewcontroller.swift:

import UIKit

class AudiUICollectionViewController: UICollectionViewController {

var audiPictures01:[carData] = [
                              carData(name:"01", picture:"1"),
                              carData(name:"02", picture:"2"),
                              carData(name:"03", picture:"3"),
                              carData(name:"04", picture:"4"),
                              carData(name:"05", picture:"5"),
                              carData(name:"06", picture:"6")]

var audiPictures02:[carData] = [
                              carData(name:"07", picture:"7"),
                              carData(name:"08", picture:"8"),
                              carData(name:"09", picture:"9"),
                              carData(name:"10", picture:"10"),
                              carData(name:"11", picture:"11"),
                              carData(name:"12", picture:"12")
                              ]


override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 2
}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if section == 0 {
        return audiPictures01.count
    } else {
        return audiPictures02.count
    }

}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    if indexPath.section == 0 {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! AudiUICollectionViewCell

        cell.collectionPicture.image = UIImage(named: audiPictures01[indexPath.row].picture)
        return cell
    } else {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! AudiUICollectionViewCell

        cell.collectionPicture.image = UIImage(named: audiPictures02[indexPath.row].picture)
        return cell
    }


}

//I don't know how to set the segue to pass the data

}

AudiDetailViewController.swift:
import UIKit

class audiDetailViewController: UIViewController {

@IBOutlet weak var audiDetailImageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    }
}

最佳答案

您可以使用prepareForSegue()功能在控制器之间从父级到子级的数据,或者使用detailViewController的另一种方法,我将prepareForSegue()添加到您的代码中,应该如下所示

 import UIKit

 class AudiUICollectionViewController: UICollectionViewController {

 var audiPictures01:[carData] = [
                          carData(name:"01", picture:"1"),
                          carData(name:"02", picture:"2"),
                          carData(name:"03", picture:"3"),
                          carData(name:"04", picture:"4"),
                          carData(name:"05", picture:"5"),
                          carData(name:"06", picture:"6")]

 var audiPictures02:[carData] = [
                          carData(name:"07", picture:"7"),
                          carData(name:"08", picture:"8"),
                          carData(name:"09", picture:"9"),
                          carData(name:"10", picture:"10"),
                          carData(name:"11", picture:"11"),
                          carData(name:"12", picture:"12")
                          ]


override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 2
}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
    return audiPictures01.count
} else {
    return audiPictures02.count
}

}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

if indexPath.section == 0 {
    let cell =          collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! AudiUICollectionViewCell

    cell.collectionPicture.image = UIImage(named: audiPictures01[indexPath.row].picture)
    return cell
} else {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! AudiUICollectionViewCell

    cell.collectionPicture.image = UIImage(named:       audiPictures02[indexPath.row].picture)
    return cell
}


}

//I don't know how to set the segue to pass the data
// use this line of code in your view controller

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    if let detailVC = segue.destinationViewController as? audiDetailViewController {
        if segue.identifier == "showDetailViewController" {
            let selectedRow = self.collectionView!.indexPathForCell((sender as! UICollectionViewCell))
            var category: carData?
            if selectedRow!.section == 0 {
                category = audiPictures01[selectedRow!.row]
            } else {
                category = audiPictures02[selectedRow!.row]
            }
            detailVC.name = category.name
            detailVC.picture = category.picture
        }
    }

}

}

在DetailViewController中,您应该为name和picture创建一个变量,或者您可以只为自己的对象创建一个变量来将数据传递给它。
我只为“name”和“picture”创建了两个变量,而detailController如下所示
import UIKit

class audiDetailViewController: UIViewController {

@IBOutlet weak var audiDetailImageView: UIImageView!
var picture: String?
var name: String?

override func viewDidLoad() {
    super.viewDidLoad()
         print(name, picture)
    }
}

希望这会有帮助

10-02 05:42
查看更多