将表视图添加到拆分视图控制器

时间:2017-07-20 16:52:21

标签: ios swift xcode uitableview uisplitviewcontroller

我正在制作闪存卡应用。现在我正在尝试为应用程序创建设置页面。我正在使用分割视图控制器,它将显示所有不同的单词组,每个组都有一个开关开关 enter image description here

如您所见,有11个不同的单词组。当我单击单词组时,我希望它显示拆分视图控制器右侧该组中的所有单词。在上图中,您可以看到第一组中包含的单词,但它们都显示在一行中。这就是我想为此使用表视图的原因。这是我遇到麻烦的地方。我不确定如何去做这件事。

对于我的Split视图控制器,我让它运行3个不同的文件 第一个文件是MasterViewController,我将发布下面的代码

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)
    }

}
}

接下来我有我的详细信息类,只要在分割视图控制器的左侧按下按钮,就会刷新UI。

 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
}
}

最后,我有一个文件,为每个" word"提供特征。

import UIKit




class Word {
let name: String
let description: String




init(name: String, description: String) {
    self.name = name
    self.description = description


}



   }

0 个答案:

没有答案