我是Swift的新手。我正在尝试在详细视图中显示图像,此图像来自UICollectionViewController
。我的问题是:
if let indexPath = collectionView.indexPathForSelectedRow...
那么正确的方法是什么?UICollectionViewController
不同部分的图片,然后将此信息传递给DetailView
?AudiUICollectionViewController.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()
}
}
答案 0 :(得分:0)
你可以使用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中,您应该为名称和图片创建一个var,或者您可以只创建一个自己对象的变量来将数据传递给它。 我只为'name'创建2 var,为'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)
}
}
希望这会有所帮助