swift ios如何从集合视图中访问数据

时间:2018-04-09 05:53:09

标签: ios swift collectionview

我在集合视图中有以下代码及其结果,我想知道的是如何从我的集合视图访问这些数据到我的函数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  ?? "" )!")

3 个答案:

答案 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
        }
    }
相关问题