将自定义标签添加到拆分视图中的原型单元格



我正在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将第二个字符串分配给detailTextLabeltext 属性

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

相关内容

  • 没有找到相关文章

最新更新