UITableView Segue,从字典传递数据

时间:2017-06-27 18:20:57

标签: swift uitableview dictionary segue

我想使用名为items的字典将数据从InvatationsViewController传递到QAVC(ViewController)。如何将数据从项目字典传递到QAVC ViewController?

InvatationsViewController:

    import UIKit

class InvatationsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var welcomeLbl: UILabel!

let segueID = "AnsweringSession"
var sendSelectedData = NSString()

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.delegate = self
    self.tableView.dataSource = self
}

// This function is called before the segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    // get a reference to the second view controller
    let secondViewController = segue.destination as! QAVC

    // set a variable in the second view controller with the data to pass

   //Pass data to QAVC:
   let item = items[indexPath.row]

    secondViewController.question1 = item["question1"]!
    secondViewController.question2 = item["question2"]!
    secondViewController.question3 = item["question3"]!
    secondViewController.question4 = item["question4"]!
    secondViewController.question5 = item["question5"]!
    secondViewController.answer1 = item["answer1"]!
    secondViewController.answer2 = item["answer2"]!
    secondViewController.answer3 = item["answer3"]!
    secondViewController.answer4 = item["answer4"]!
    secondViewController.answer5 = item["answer5"]!

}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! QuizTableViewCell

    let item = items[indexPath.row]

    cell.nameLbl?.text = item["name"]!
    cell.modeLbl?.text = item["mode"]!
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let item = items[indexPath.row]
    self.performSegue(withIdentifier: segueID, sender: item)
}


@IBAction func createMap(_ sender: UIButton) {

}

}

QAVC:

    import UIKit

class QAVC: UIViewController {

var question1 = ""
var question2 = ""
var question3 = ""
var question4 = ""
var question5 = ""
var answer1 = ""
var answer2 = ""
var answer3 = ""
var answer4 = ""
var answer5 = ""

@IBOutlet weak var questionLbl: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    questionLbl.text = question1
}


}

项目Dictonary:

    var items = [
["name": "Animals", "mode": "5 Mode",
 "question1": "Random QuestionA", "answer1": "false",
 "question2": "Random QuestionB", "answer2": "true",
 "question3": "Random QuestionC", "answer3": "true",
 "question4": "Random QuestionD", "answer4": "true",
 "question5": "Random QuestionE", "answer5": "false"],
 ["name": "Capitals", "mode": "5 Mode",
 "question1": "Random QuestionF", "answer1": "false",
 "question2": "Random QuestionG", "answer2": "true",
 "question3": "Random QuestionH", "answer3": "true",
 "question4": "Random QuestionI", "answer4": "true",
 "question5": "Random QuestionK", "answer5": "false"]]

1 个答案:

答案 0 :(得分:1)

didSelectRow方法

之外定义您的项目
var item: Dictionary<String, String>!

然后在didSelectRow中,将值设置为item变量

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        item = items[indexPath.row]
        self.performSegue(withIdentifier: segueID, sender: item)
    }

最后你可以传递选定的

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    secondViewController.question1 = item["question1"]!
    secondViewController.question2 = item["question2"]!
    secondViewController.question3 = item["question3"]!
    secondViewController.question4 = item["question4"]!
    secondViewController.question5 = item["question5"]!
    secondViewController.answer1 = item["answer1"]!
    secondViewController.answer2 = item["answer2"]!
    secondViewController.answer3 = item["answer3"]!
    secondViewController.answer4 = item["answer4"]!
    secondViewController.answer5 = item["answer5"]!

}