[英]Adding a custom label to a prototype cell in a split view
我正在使用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
}
style
設置為Right Detail
, Left Detail
或Subtitle
,它啟用detailTextLabel
標簽。 cellForRowAt
將第二個字符串分配給detailTextLabel
的text
屬性 如果預定義樣式不符合您的需要,則將樣式設置為custom
,將所有UI元素拖動到單元格,添加約束,創建UITableViewCell
子類,添加IBOutlets
,連接出口,將單元格的類設置為自定義類和將cellForRowAt
出列的單元格轉換為自定義類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.