I have an app the consists of many different questions in the form of arrays. I put these arrays into different swift files i.e. QuestionsFile1.swift, QuestionsFile 2.swift, QuestionFile3.swift, QuestionFile4.swift....etc.
I have a UITableView that is shows these various sections in each cell of the table view i.e. Questions1, Questions2, Questions3, Questions4...etc.
I have it set up so that when the UITableViewCell is selected it will display the viewController associated with the swift file. However this means I have made a lot of viewController on the storyBoard.
How can I just have one viewController and populate it with questions from the different swift files depending on which UITableViewCell that was selected?
I select call the UIViewControllers using
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == 0 || indexPath == 1 {
self.performSegueWithIdentifier("QuestionsFile1", sender: self)
}
if indexPath.row == 1 {
self.performSegueWithIdentifier("QuestionsFile2", sender: self)
}
if indexPath.row == 2 {
self.performSegueWithIdentifier("QuestionsFile3", sender: self)
}
if indexPath.row == 3 {
self.performSegueWithIdentifier("QuestionsFile4", sender: self)
}
if indexPath.row == 4 {
self.performSegueWithIdentifier("QuestionsFile5", sender: self)
}
if indexPath.row == 5 {
self.performSegueWithIdentifier("QuestionsFile6", sender: self)
}
if indexPath.row == 6 {
self.performSegueWithIdentifier("QuestionsFile7", sender: self)
}
}
I just want to call one view controller but then populate it with the questions in the various swift files depending on the UITableViewCell selected. Because I have a lot of viewControllers when I think I could just have one.
Yes, you're right - it looks like all you are changing in the question file tables is the content, so you should have a single view with a tableView, and populate it based on the questions you want.
You should probably store your questions in data files rather than hard-coding them as swift classes, so look into reading and parsing text files in your app bundle, or one of the many light-weight databases - Realm or (possibly overkill) CoreData.