我需要使用数组将数据从 UITableViewController 传递到 UIViewController

Posted

技术标签:

【中文标题】我需要使用数组将数据从 UITableViewController 传递到 UIViewController【英文标题】:I need to pass data from UITableViewController to UIViewController using Array 【发布时间】:2018-08-08 13:34:48 【问题描述】:

这是我第一次构建自己的应用程序,我需要帮助,该应用程序是关于测验的。

应用有很多素材,每种素材都有很多测验,这是QuizzesVC

我想从 QuizzesArray 传递数据(代码在下面) 到 QuestionVC

例如,当点击数学部分中的 Quiz 1 单元格时,QuestionVC 应显示数学数组的第一部分:

[Quiz(title: "Quiz1", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)]

QuizzesVC代码:

class QuizesVC: UITableViewController 

//...


override func viewDidLoad() 
    super.viewDidLoad()
    navigationItem.title = "Quizes"
    navigationController?.navigationBar.prefersLargeTitles = true


// MARK: - Table view data source



override func numberOfSections(in tableView: UITableView) -> Int 
    //...
    
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

    //...

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? 
    //...


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    //...

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 


这是 QuestionVC 代码:

    class QuestionVC : UIViewController 
    @IBOutlet var questionLabel: UILabel!//QuizzesArray: ques
    @IBOutlet var choiceAButton: UIButton!//QuizzesArray: choiceA
    @IBOutlet var choiceBButton: UIButton!//QuizzesArray: choiceB
    @IBOutlet var choiceCButton: UIButton!//QuizzesArray: choiceC

这是QuizzesArray的代码:

class QuizzesArray 
var mathQuizes = [[Quiz(title: "Quiz1", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)],
[Quiz(title: "Quiz2", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)],
[Quiz(title: "Quiz3", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)],
[Quiz(title: "Quiz4", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)],
[Quiz(title: "Quiz5", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)],
[Quiz(title: "Quiz6", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)]
]
var chemistryQuizes = [[Quiz(title: "Quiz1", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)],
                           [Quiz(title: "Quiz2", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)],
                           [Quiz(title: "Quiz3", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)],
                           [Quiz(title: "Quiz4", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)],
                           [Quiz(title: "Quiz5", ques: "1+2", choiceA: "3", choiceB: "2", choiceC: "4", correctAnswer: choiceA)]
    ]

struct Quiz 
var title : String
var ques : String
var choiceA : String
var choiceB : String
var choiceC : String
var correctAnswer : Int

如果您需要更多解释,请告诉我。

【问题讨论】:

【参考方案1】:

在 didSelectRowAt 中你可以得到单元格的索引:

let itemToPass = mathQuizes[indexPath.row]

比执行你的转场:

performSegue(withIdentifier: "yourSegue", sender: itemToPass)

我在上面解释过的完整代码:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
    let itemToPass = mathQuizes[indexPath.row]
    performSegue(withIdentifier: "yourSegue", sender: itemToPass)

所以您现在已经准备好使用

将您的项目传递给您的下一个视图控制器
override func prepare(for segue: UIStoryboardSegue, sender: Any?)  
     if segue.identifier == "yourSegue" 
        if let quiz = sender as? Quiz 
           let destination = segue.destination as! NextViewController
           destination.quiz = quiz 
        
     

【讨论】:

谢谢Alstar,但我还是初学者,请您详细说明一下 他们在这一行显示错误destination.quiz = quiz // Value of type 'QuestionVC' has no member 'quiz' 当然,这只是一个例子,你必须像这样在你的 nextviewcontroller 中设置一个变量:var quiz: Quiz!,然后你才能使用你的 destination.quiz

以上是关于我需要使用数组将数据从 UITableViewController 传递到 UIViewController的主要内容,如果未能解决你的问题,请参考以下文章

Swift Plist 读取和初始化数组作为键的对象

使用公式将数组从 VBA 函数输出到 Excel 工作表

Tableview没有重新加载数据

如何使用 axios 将数组从 javascript 传递到 laravel 控制器

如何正确使用 Angular 中的 Observable 将数组数据从父级发送到子级?

将核心数据行从一个表(实体)映射到 [CLLocation] 数组?