【问题标题】:UITableView Segue, Pass Data From DictionaryUITableView Segue,从字典中传递数据
【发布时间】:2017-06-27 18:20:56
【问题描述】:

我想使用名为 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
}


}

项目词典:

    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"]]

【问题讨论】:

  • 由于您正在更新您的问题,您的items 数组中有两个字典。你愿意将哪一个传递给下一个视图控制器?
  • 不相关,但您是否考虑过比字典更合适的数据格式?如果不传递整个字典而不是 10 个提取值?
  • @FangmingNing 我试图传递用户从 tableview 中选择的那个

标签: swift uitableview dictionary segue


【解决方案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"]!

}

【讨论】:

  • 感谢您的回答,但当按下相应的 tableview 单元格时,我需要这个let item = items[indexPath.row] 来回答问题。我收到此错误未解析标识符indexPathlet item = items[indexPath.row] 工作于 cellForRowAt 方法
  • @articga 已更新。展望未来,您可以传递字典或创建结构、对象来替换您的字典,以使您的语法更清晰
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多