Files
language4english/LearnEnglish/LearnEnglish/Views/WordListViewController.swift
T
2026-07-18 22:24:12 +08:00

60 lines
2.5 KiB
Swift

import UIKit
final class WordListViewController: UITableViewController {
private let category: WordCategory
init(category: WordCategory) {
self.category = category
super.init(style: .plain)
}
@available(*, unavailable)
required init?(coder: NSCoder) { fatalError() }
override func viewDidLoad() {
super.viewDidLoad()
title = category.name
navigationItem.largeTitleDisplayMode = .never
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
let quizMenu = UIMenu(title: "", children: [
UIAction(title: QuizMode.enToZh.rawValue) { [weak self] _ in self?.startQuiz(.enToZh) },
UIAction(title: QuizMode.zhToEn.rawValue) { [weak self] _ in self?.startQuiz(.zhToEn) },
UIAction(title: QuizMode.spelling.rawValue) { [weak self] _ in self?.startQuiz(.spelling) }
])
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "测验", menu: quizMenu)
}
private func startQuiz(_ mode: QuizMode) {
if category.words.count <= 10 {
let config = QuizConfig(mode: mode, words: category.words)
navigationController?.pushViewController(QuizViewController(config: config), animated: true)
} else {
let request = QuizSetupRequest(mode: mode, words: category.words)
navigationController?.pushViewController(QuizSetupViewController(request: request), animated: true)
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
category.words.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let word = category.words[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
var content = cell.defaultContentConfiguration()
content.text = word.word
content.textProperties.font = .preferredFont(forTextStyle: .headline)
content.secondaryText = word.meaning
content.secondaryTextProperties.color = .secondaryLabel
cell.contentConfiguration = content
cell.accessoryType = .disclosureIndicator
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
navigationController?.pushViewController(WordDetailViewController(word: category.words[indexPath.row]), animated: true)
}
}