我正在使用SplitViewController中的字典应用程序。在主视图控制器上,我想显示各种输入单词及其被点击的次数,而在细节上,我想要显示单词的定义和细节。到目前为止,我已经完成了所有设置,但要敲击单词的次数应该显示在主视图控制器上。如何在主视图控制器中自定义各种标签并添加标签?

// "Word" class
import UIKit

class Word {
let name: String
let meaning: String
let wordType: String
let numberOfTimesFound: String


init(name: String, meaning: String, wordtype: String, numberOfTimesFound: String) {
    self.name = name
    self.meaning = meaning
    self.wordType = wordtype
    self.numberOfTimesFound = numberOfTimesFound
    }
}


let words = [
Word(name: "Afraid", meaning: "WORD DEFINITION GOES HERE", wordtype: "adjective", numberOfTimesFound: "1")
]

//MasterViewController.swift

import UIKit

class MasterViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()


    let firstWord = words.first

}


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return words.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    let word = words[indexPath.row]
    cell.textLabel?.text = word.name
    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let word = words[indexPath.row]
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.detailViewController.refreshUI(word: word)
}

//AppDelegate.swift

import UIKit

@UIApplicationMain


class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var masterViewController = MasterViewController()
var detailViewController = DetailViewController()

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

    // Override point for customization after application launch.
    return true

}

最佳答案

在项目导航器中选择情节提要或笔尖。
选择原型单元。
按⌥⌘4以显示“属性”检查器。
将单元格的style设置为Right DetailLeft DetailSubtitle,这将启用detailTextLabel标签。
cellForRowAt中,将第二个字符串分配给textdetailTextLabel属性。




如果预定义的样式无法满足您的需要,请将样式设置为custom,将所有UI元素拖到单元格,添加约束,创建UITableViewCell子类,添加IBOutlets,连接插座,设置样式。单元格转换为自定义类,并将cellForRowAt中出队的单元格转换为自定义类。

07-24 09:50
查看更多