0

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.
enter image description here

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


}



   }
Anthony Rubin
  • 69
  • 1
  • 1
  • 14
  • What exactly are you trying to achieve? If you break the problem into something smaller and concrete, it will be easier for others to help. (Example: I want to display the text from the selected table view cell on a label in the detail view controller.) – nathangitter Jul 20 '17 at 19:05
  • 1
    if you look at mainViewController where it says self.words.append(Word(name: "initial /l/ 1 syllable", description: "lake lamb lamp lark leaf leash left leg")) . I want each word in the description to be displayed in a cell in a table view instead of having a long horizontal list which is what it currently is. The words displayed will change depending on which table view cell on the left hand side is selected. – Anthony Rubin Jul 20 '17 at 19:09
  • So you want an additional table view in the detail view controller? – nathangitter Jul 20 '17 at 19:27
  • 1
    yes, to display the contents of the "description" – Anthony Rubin Jul 20 '17 at 19:28
  • It looks like you already have the code to set up a table view, so just take the same approach in the detail view controller. I'd also recommend splitting the long string into an array of strings, with one word per string. This will make it easier to display the data in a table view. – nathangitter Jul 20 '17 at 19:30

0 Answers0