diff --git a/Note/ViewController.swift b/Note/ViewController.swift index 91691d5..70df32d 100644 --- a/Note/ViewController.swift +++ b/Note/ViewController.swift @@ -9,11 +9,75 @@ import UIKit class ViewController: UIViewController { - override func viewDidLoad() { - super.viewDidLoad() - // Do any additional setup after loading the view. + private var tableView: UITableView! + + private struct MenuItem { + let icon: String + let iconColor: UIColor + let title: String } + private let menuItems: [[MenuItem]] = [ + [MenuItem(icon: "note.text", iconColor: .systemYellow, title: "打开备忘录")], + [MenuItem(icon: "doc.text", iconColor: .systemBlue, title: "格式化日志")], + [MenuItem(icon: "text.alignleft", iconColor: .systemGreen, title: "格式化笔记")] + ] + override func viewDidLoad() { + super.viewDidLoad() + setupUI() + } + + private func setupUI() { + title = "Note" + view.backgroundColor = .systemGroupedBackground + + tableView = UITableView(frame: .zero, style: .insetGrouped) + tableView.translatesAutoresizingMaskIntoConstraints = false + tableView.delegate = self + tableView.dataSource = self + tableView.register(UITableViewCell.self, forCellReuseIdentifier: "menuCell") + tableView.backgroundColor = .systemGroupedBackground + view.addSubview(tableView) + + NSLayoutConstraint.activate([ + tableView.topAnchor.constraint(equalTo: view.topAnchor), + tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor), + tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor), + tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor) + ]) + } } +extension ViewController: UITableViewDataSource { + + func numberOfSections(in tableView: UITableView) -> Int { + return menuItems.count + } + + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return menuItems[section].count + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath) + let item = menuItems[indexPath.section][indexPath.row] + + var config = cell.defaultContentConfiguration() + config.text = item.title + config.image = UIImage(systemName: item.icon) + config.imageProperties.tintColor = item.iconColor + config.imageToTextPadding = 12 + cell.contentConfiguration = config + cell.accessoryType = .disclosureIndicator + + return cell + } +} + +extension ViewController: UITableViewDelegate { + + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + tableView.deselectRow(at: indexPath, animated: true) + } +}