0

I am working on a dictionary app in a SplitViewController. On the master view controller I want to show the various entry words AND the number of times that they've been tapped, while on the detail I want to show the word definition and details. I've set up everything so far, except the number of times that a word has been tapped, which should be showing on the Main view controller. How do I manage to customise the various labels in the main view controller, adding a label?

// "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

1 回答 1

0
  • 在项目导航器中选择故事板或 nib。
  • 选择原型单元格。
  • ⌥⌘4显示属性检查器。
  • style单元格的 设置为Right DetailLeft DetailSubtitle启用detailTextLabel标签。
  • cellForRowAt将第二个字符串分配textdetailTextLabel

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

于 2019-06-08T09:13:09.723 回答