gpt4 book ai didi

ios - 将 TableView 添加到详细 View Controller

转载 作者:行者123 更新时间:2023-11-30 12:17:16 24 4
gpt4 key购买 nike

我正在制作我的第一个应用程序。现在我正在尝试制作一个设置页面。到目前为止看起来是这样的。 enter image description here

我将分割 View Controller 连接到主视图 Controller 和详细 View Controller (代码如下)。如您所见,现在 Split View Controller 的左侧有一个包含不同按钮的 TableView 。当按下每个按钮时,我希望它在 View Controller 的右侧显示单词列表。它确实已经显示了单词列表,但我希望它在表格 View 中显示水平堆叠的单词,但我不知道如何执行此操作。

//
// 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。如果您查看 MasterViewController 代码,您会看到其中显示“描述”。描述中包含的列表应该显示在右侧的表格 View 中。

最佳答案

在masterviewcontroller的func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)中,需要替换viewcontroller。从 appdelegate 获取 splitviewcontroller 实例,现在向 splitview Controller View Controller 属性分配所需的 View Controller 对象,并确保所需的 View Controller 对象具有导航 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
}

关于ios - 将 TableView 添加到详细 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45249741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com