I am in the process of creating a flash card app. Right now I am trying to create a settings page for the app. I am using a split view controller which will display all the different word groups and there will be an on and off switch for each group.
as you can see, there are 11 different word groups. When I click on the word group I want it to display all of the words in that group on the right side of the split view controller. On the picture above you see the words contained in the first group but they are all displayed in a single line. This is why I want to use a table view for this. This is where i am having trouble. I am not exactly sure how to go about doing this.
For my Split view controller I have it running with 3 different files.
The first file is the MasterViewController, I will post the code below
import UIKit
protocol WordSelectionDelegate: class {
func wordSelected(newWord: Word)
}
class MasterViewController: UITableViewController {
var words = [Word]()
weak var delegate: WordSelectionDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.words.append(Word(name: "initial /l/ 1 syllable", description: "lake lamb lamp lark leaf leash left leg lime lion lips list lock log look love lunch"))
self.words.append(Word(name: "initial /l/ multisyllabic", description: ""))
self.words.append(Word(name: "intersyllabic /l/", description: ""))
self.words.append(Word(name: "final /l/", description: ""))
self.words.append(Word(name: "initial /pl/", description: ""))
self.words.append(Word(name: "initial /bl/", description: ""))
self.words.append(Word(name: "initial /fl/", description: ""))
self.words.append(Word(name: "initial /gl/", description: ""))
self.words.append(Word(name: "initial /kl/", description: ""))
self.words.append(Word(name: "initial /sl/", description: ""))
self.words.append(Word(name: "final /l/ clusters", description: ""))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return self.words.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
// Configure the cell...
let word = self.words[indexPath.row]
cell.textLabel?.text = word.name
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt
indexPath: IndexPath) {
let selectedMonster = self.words[indexPath.row]
self.delegate?.wordSelected(newWord: selectedMonster)
if let Detail = self.delegate as? Detail {
splitViewController?.showDetailViewController(Detail, sender: nil)
}
}
}
Next I have my detail class, this refreshes the UI whenever a button is pressed on the left side of the split view controller.
import UIKit
class Detail: UIViewController {
@IBOutlet weak var descriptionLabel: UILabel!
var word: Word! {
didSet (newWord) {
self.refreshUI()
}
}
func refreshUI() {
descriptionLabel?.text = word.description
}
override func viewDidLoad() {
super.viewDidLoad()
refreshUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension Detail: WordSelectionDelegate {
func wordSelected(newWord: Word) {
word = newWord
}
}
Finally I have a file which gives characteristics to each "word".
import UIKit
class Word {
let name: String
let description: String
init(name: String, description: String) {
self.name = name
self.description = description
}
}