ios - 在分割 View 中向原型(prototype)单元添加自定义标签

标签 ios swift uilabel uisplitviewcontroller uisplitview

我正在 SplitViewController 中开发字典应用程序。在主视图 Controller 上,我想显示各种输入单词以及它们被点击的次数,而在细节上,我想显示单词定义和详细信息。到目前为止,我已经设置了所有内容,除了一个单词被点击的次数,它应该显示在主视图 Controller 上。如何在主视图 Controller 中自定义各种标签,添加标签?

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

}

最佳答案

  • 在项目导航器中选择 Storyboard或 Nib 。
  • 选择原型(prototype)单元。
  • ⌥⌘4 显示属性检查器。
  • 将单元格的样式设置为Right DetailLeft DetailSubtitle,以启用detailTextLabel 标签。
  • cellForRowAt 中将第二个字符串分配给 detailTextLabeltext 属性
<小时/>

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

关于ios - 在分割 View 中向原型(prototype)单元添加自定义标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56504903/

相关文章:

objective-c - 是否可以从iOS应用程序中的麦克风获得声压?

ios - 如何在 AppDelegate 之外处理 UIApplicationDidRegisterToRemoteNoticationsWithDeviceToken?

ios - 由于异步,在发布后连接到 MQTT

iphone - 如何在 iPhone 中的标签值中添加或连接百分号

ios - 如何渲染离屏 IOS

java - 无法使用私钥签署数据,获取 swift 中 key 不支持的算法

ios - 符号化包含 libswiftcore.dylib 的 iOS 崩溃日志行

ios - 从 Swift 函数中的异步调用返回数据

ios - NSLineBreakByWordWrapping中断哪些字符?

ios - 其他 View 上的 UILabel 换行符