简体   繁体   English

将表视图添加到详细信息视图控制器

[英]Adding table view to detail view controller

I am in the process of making my first app. 我正在制作我的第一个应用程序。 Right now I am trying to make a settings page. 现在,我正在尝试进入设置页面。 So far it looks like this. 到目前为止看起来像这样。 在此处输入图片说明

I have the split view controller hooked up to a master view controller and a detail view controller (code below). 我已将拆分视图控制器连接到主视图控制器和详细视图控制器(下面的代码)。 As you can see, right now there is a table view on the left side of the split view controller containing different buttons. 如您所见,现在在拆分视图控制器的左侧有一个包含不同按钮的表格视图。 When each button is pressed I want it to display a list of words on the right side of the view controller. 按下每个按钮时,我希望它在视图控制器的右侧显示一个单词列表。 It does display a list of words already but I want it to display the words stacked horizontally in a table view and I am not sure how to go about doing this. 它确实已经显示了一个单词列表,但是我希望它在表格视图中显示水平堆叠的单词,但我不确定该怎么做。

//
//  MasterViewController.swift
//  firstapp
//
//  Created by Anthony Rubin on 7/18/17.
//  Copyright © 2017 rubin. All rights reserved.
//

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: "words, words, words, words"))

    self.words.append(Word(name: "intersyllabic /l/", description: "words, words, words, words"))

    self.words.append(Word(name: "final /l/", description: "words, words, words, words"))

    self.words.append(Word(name: "initial /pl/", description: "words, words, words, words"))

    self.words.append(Word(name: "initial /bl/", description: "words, words, words, words"))

    self.words.append(Word(name: "initial /fl/", description: ""))

    self.words.append(Word(name: "initial /gl/", description: "words, words, words, words"))

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

}

__ __

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

-- -

class Word {
let name: String
let description: String




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


}
}

-- -

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    let splitViewController = self.window!.rootViewController as! UISplitViewController
    let leftNavController = splitViewController.viewControllers.first as! UINavigationController
    let MasterViewController = leftNavController.topViewController as! MasterViewController
    let Detail = splitViewController.viewControllers.last as! Detail

    let firstWord = MasterViewController.words.first
    Detail.word = firstWord

    MasterViewController.delegate = Detail

    return true
}

PS. PS。 If you look at the MasterViewController code you will see where it says "description". 如果您查看MasterViewController代码,您将在其上看到“说明”。 the lists contained in description is what should be displayed in the table view on the right. 描述中包含的列表应显示在右侧的表格视图中。

In the func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) , of the masterviewcontroller, you need to replace the viewcontroller. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)中,您需要替换视图控制器。 Get the splitviewcontroller instance from the appdelegate, now to the splitview controllers view controller property assign your desired viewcontroller object, and make sure your desired view controller object has a navigation controller. 从appdelegate中获取splitviewcontroller实例,现在为splitview controllers的view controller属性分配了所需的viewcontroller对象,并确保所需的view controller对象具有导航控制器。

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.row == 1 {
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let splitViewController = appDelegate.window!.rootViewController as! UISplitViewController
        let nextViewController = storyBoard.instantiateViewController(withIdentifier: "yourviewcontrollername") as! UINavigationController
        splitViewController.viewControllers[1] = nextViewController
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM