[ToyProject]ToDoList(登録保留中)
22800 ワード
🍎 To Do List
🍏 機能の詳細
🍏 テクノロジーを活用
🍏 機能実装
FullCode_github_ToDoList
1.基礎UI設計
作成
2.コード操作
実装
@IBAction func tabAddButton(_ sender: UIBarButtonItem) {
let alert = UIAlertController(title: "MustDo", message: nil, preferredStyle: .alert)
let registerButton = UIAlertAction(title: "등록", style: .default, handler: { [weak self] _ in
guard let title = alert.textFields?[0].text else { return }
let task = Task(title: title, done: false)
self?.tasks.append(task)
self?.mustDoTableView.reloadData()
})
let cancelButton = UIAlertAction(title: "취소", style: .cancel, handler: nil)
alert.addAction(cancelButton)
alert.addAction(registerButton)
alert.addTextField(configurationHandler: { textField in
textField.placeholder = "할 일을 입력해주세요."
})
self.present(alert, animated: true, completion: nil)
}
UItableViewデータソースは、テーブルビューオブジェクトの作成と変更に必要な情報を提供します.
非伝統的な方法を実施しなければならない.
UItableViewDelegateでは、テーブルビューの表示部分の設定、行のアクションの管理、添付ファイルビューのサポート、およびテーブルビュー内の行の編集を行うことができます.
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tasks.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let task = self.tasks[indexPath.row]
cell.textLabel?.text = task.title
if task.done {
cell.accessoryType = .checkmark
} else {
cell.accessoryType = .none
}
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
self.tasks.remove(at: indexPath.row)
self.mustDoTableView.deleteRows(at: [indexPath], with: .automatic)
if self.tasks.isEmpty {
self.tabDoneButton()
}
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
var tasks = self.tasks
let task = tasks[sourceIndexPath.row]
tasks.remove(at: sourceIndexPath.row)
tasks.insert(task, at: destinationIndexPath.row)
self.tasks = tasks
}
}
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
var task = self.tasks[indexPath.row]
task.done = !task.done
self.tasks[indexPath.row] = task
self.mustDoTableView.reloadRows(at: [indexPath], with: .automatic)
}
}
func saveTasks() {
let data = self.tasks.map {
[
"title": $0.title,
"done": $0.done
]
}
let userDefaults = UserDefaults.standard
userDefaults.set(data, forKey: "tasks")
}
func loadTasks() {
let userDefaults = UserDefaults.standard
guard let data = userDefaults.object(forKey: "tasks") as? [[String: Any]] else { return }
// "tasks"에 대한 value가 Any?타입으로 반환되는데, 이 Any를 [[String: Any]] 로 타입캐스팅한 것.
self.tasks = data.compactMap {
guard let title = $0["title"] as? String else { return nil }
guard let done = $0["done"] as? Bool else { return nil }
return Task(title: title, done: done)
}
}
🍏 に感銘を与える
Reference
この問題について([ToyProject]ToDoList(登録保留中)), 我々は、より多くの情報をここで見つけました https://velog.io/@swiftist9891/ToyProject-ToDoList할-일-등록テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol