iOS的简单示例
示例:简单的待办事项列表
struct Todo {
var title: String
var isCompleted: Bool
}import UIKit
class TodoViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var todos: [Todo] = [
Todo(title: "Buy groceries", isCompleted: false),
Todo(title: "Walk the dog", isCompleted: false),
Todo(title: "Read a book", isCompleted: true)
]
let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
title = "Todos"
view.addSubview(tableView)
tableView.frame = view.bounds
tableView.dataSource = self
tableView.delegate = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return todos.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let todo = todos[indexPath.row]
cell.textLabel?.text = todo.title
cell.accessoryType = todo.isCompleted ? .checkmark : .none
return cell
}
}总结
Last updated