情人節要做的事

情人節到了,今天要做的清單建好了嗎?剛好可以練習ToDoList,就來看看我的作品吧!如果不喜歡預設的清單還可以自己新增刪除喔~

import UIKitclass ToDoListTableViewController: UITableViewController {
var ToDoList = ["鮮花","紅包","約會","燭光晚餐","鴛鴦浴","床上運動","相擁而眠"]
var isFinishd: [Bool]?
override func viewDidLoad() {
super.viewDidLoad()
isFinishd = Array(repeating: false, count: ToDoList.count)
}
@IBAction func unwindToDoListTableView(segue: UIStoryboardSegue){
if let sourceController = segue.source as? DetailTableViewController, let todo = sourceController.todo{
ToDoList.insert(todo, at: 0)
let indexPath = IndexPath(row: 0, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic)
}
}


override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ToDoList.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoListCellId", for: indexPath)
let row = indexPath.row
cell.textLabel?.text = ToDoList[row]
if isFinishd![row]{
cell.accessoryType = .checkmark
}else{
cell.accessoryType = .none
}
return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
isFinishd![indexPath.row] = !isFinishd![indexPath.row]
tableView.reloadData()
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
ToDoList.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
}
}
import UIKitclass DetailTableViewController: UITableViewController {@IBOutlet weak var ToDoTextField: UITextField!

override func viewDidLoad() {
super.viewDidLoad()
}
var todo:String?


override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if ToDoTextField.text?.isEmpty == false {
return true
}else{
let alertController = UIAlertController(title: "Error", message: "Input error", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alertController, animated: true, completion: nil)
return false
}
}

@IBAction func closekeyboard(_ sender: Any) {
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
todo = ToDoTextField.text!
}
}

GitHub

--

--