Ios 向拆分视图控制器添加表视图
我正在创建一个闪存卡应用程序。现在我正在尝试为应用程序创建一个设置页面。我使用的是一个分割视图控制器,它将显示所有不同的词组,每个词组都有一个开关。Ios 向拆分视图控制器添加表视图,ios,swift,xcode,uitableview,uisplitviewcontroller,Ios,Swift,Xcode,Uitableview,Uisplitviewcontroller,我正在创建一个闪存卡应用程序。现在我正在尝试为应用程序创建一个设置页面。我使用的是一个分割视图控制器,它将显示所有不同的词组,每个词组都有一个开关。 如你所见,有11个不同的词组。单击单词组时,我希望它在拆分视图控制器的右侧显示该组中的所有单词。在上图中,您可以看到第一组中包含的单词,但它们都显示在一行中。这就是为什么我想使用一个表视图。这就是我遇到麻烦的地方。我不知道该怎么做 对于我的分割视图控制器,我让它与3个不同的文件一起运行。 第一个文件是MasterViewController,我将
如你所见,有11个不同的词组。单击单词组时,我希望它在拆分视图控制器的右侧显示该组中的所有单词。在上图中,您可以看到第一组中包含的单词,但它们都显示在一行中。这就是为什么我想使用一个表视图。这就是我遇到麻烦的地方。我不知道该怎么做 对于我的分割视图控制器,我让它与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)
}
}
}
接下来是我的detail类,每当按下split view控制器左侧的按钮时,它都会刷新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
}
}
最后,我有一个文件,其中给出了每个“单词”的特征
你到底想达到什么目的?如果你把问题分解成更小更具体的问题,别人会更容易帮助你。(示例:我想在详细视图控制器的标签上显示所选表格视图单元格中的文本。)如果查看mainViewController,其中显示self.words.append(单词(名称:“首字母/l/1音节”,描述:“lake lamb lamp lark leaf leash leash left leg”))。我希望描述中的每个单词都显示在表视图的单元格中,而不是像现在这样有一个长长的水平列表。显示的文字将根据选择左侧的哪个表格视图单元格而变化。因此,您希望在“详细视图”控制器中添加一个额外的表格视图吗?是的,要显示“说明”的内容,您似乎已经拥有设置表格视图的代码,因此只需在“详细视图”控制器中采用相同的方法即可。我还建议将长字符串拆分为字符串数组,每个字符串一个单词。这将使在表视图中显示数据更容易。
import UIKit
class Word {
let name: String
let description: String
init(name: String, description: String) {
self.name = name
self.description = description
}
}