gpt4 book ai didi

swift - 如何从 UICollectionView 中的多个部分传递数据

转载 作者:行者123 更新时间:2023-11-28 08:26:52 25 4
gpt4 key购买 nike

我是 Swift 的新手。我试图在详细 View 中显示图像,而该图像来自 UICollectionViewController。我的问题是:

  1. 如何通过 segue 将数据发送到详细信息 View ?我不能使用 if let indexPath = collectionView.indexPathForSelectedRow... 那么正确的方法是什么?
  2. 我如何判断用户何时点击来自 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()

}
}

最佳答案

您可以使用 prepareForSegue() 功能在您的 Controller 之间从 parent 到它的 child 或您的 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,或者您可以只为您自己的对象创建一个变量以将数据传递给它。我只创建了 2 个变量,一个用于“名称”,另一个用于“图片”,你的 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)
}
}

希望对你有帮助

关于swift - 如何从 UICollectionView 中的多个部分传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39728256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com