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

swift ios how to access data from a collection view

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

使用 indexPathsForSelectedItems 的关键字作为 colletionview

   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
    }
   }
   }

您可以将最后选择的项目数据存储在 class 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
        }
    }

也许你可以使用:

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类型。您可以相应地使用