我正在 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 Detail
、Left Detail
或Subtitle
,以启用detailTextLabel
标签。 - 在
cellForRowAt
中将第二个字符串分配给detailTextLabel
的text
属性
如果预定义样式不符合您的需求,请将样式设置为自定义
,将所有 UI 元素拖动到单元格,添加约束,创建 UITableViewCell
子类,添加IBOutlets
,连接 socket ,将单元格的类设置为自定义类,并将 cellForRowAt
中出列的单元格转换为自定义类。
关于ios - 在分割 View 中向原型(prototype)单元添加自定义标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56504903/