我在集合视图中有以下代码及其结果,我想知道的是如何从我的集合视图访问这些数据到我的函数prepare。我希望获得getTempDetails
的值
getTempDetails["name"] as! String
是我
的价值destination.chore_name = getTempDetails["name"] as! String
但我似乎无法访问它。感谢
准备代码
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let distanation = segue.destination as? ChoreDetailsViewController {
distanation.chore_name = "Hi"
}
}
代码
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
// print("You selected cell #\(indexPath.item)!")
// print("emong gepili:" , titleArray[indexPath.row])
if indexPath.row == 0 {
performSegue(withIdentifier: "goToSegue", sender: nil)
} else {
performSegue(withIdentifier: "gotoDetails", sender: nil)
print("You selected cell #\(indexPath.item)!")
if let getTempDetails: [String : Any] = getAllDetail[indexPath.row] {
print("You selected ID #\( getTempDetails["reward"] as? String ?? "" )!")
print("You selected ID #\( getTempDetails["desc"] as? String ?? "" )!")
print("You selected ID #\( getTempDetails["sched"] as? String ?? "" )!")
答案 0 :(得分:1)
可能你可以使用:
var strReward:String = String()
var strDesc:String = String()
var sreSched:String = String()
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.row == 0 {
performSegue(withIdentifier: "goToSegue", sender: nil)
} else {
performSegue(withIdentifier: "gotoDetails", sender: nil)
print("You selected cell #\(indexPath.item)!")
if let getTempDetails: [String : Any] = getAllDetail[indexPath.row] {
strReward = (getTempDetails["reward"] as? String)!
strDesc = (getTempDetails["desc"] as? String)!
sreSched = (getTempDetails["sched"] as? String)!
}
}
}
将此数据传递给下一个VC或以任何您想要的方式使用它们
注意: - 我不确定数据的数据类型是什么,我认为它是String
类型。你可以相应地使用
答案 1 :(得分:0)
使用关键字indexPathsForSelectedItems
进行收集视图
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "gotoDetails" ,
let nextScene = segue.destination as? ChoreDetailsViewController ,
let indexPath = self.yourCollectionViewName. indexPathsForSelectedItems().first {
if let getTempDetails: [String : Any] = getAllDetail[indexPath.item] {
print("You selected ID #\( getTempDetails["reward"] as? String ?? "" )!")
print("You selected ID #\( getTempDetails["desc"] as? String ?? "" )!")
print("You selected ID #\( getTempDetails["sched"] as? String ?? "" )!")
nextScene.chore_name = getTempDetails["name"] as! String
}
}
}
答案 2 :(得分:0)
您可以在类var
中存储最后选择的项目数据func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.row == 0 {
performSegue(withIdentifier: "goToSegue", sender: nil)
} else {
if let getTempDetails: [String : Any] = getAllDetail[indexPath.row],
let name = getTempDetails["name"] as? String {
selectedChoreName = name
performSegue(withIdentifier: "gotoDetails", sender: nil)
}
然后
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let distanation = segue.destination as? ChoreDetailsViewController {
distanation.chore_name = selectedChoreName
selectedChoreName = "" //optional
}
}