参考链接:https://github.com/soapyigu/Swift-30-Projects
1 import UIKit 2 3 class ToDoItem: NSObject { 4 var id: String 5 var image: String 6 var title: String 7 var date: Date 8 9 init(id: String, image: String, title: String, date: Date) { 10 self.id = id 11 self.image = image 12 self.title = title 13 self.date = date 14 } 15 }
1 import Foundation 2 3 func dateFromString(_ date: String) -> Date? { 4 let dateFormatter = DateFormatter() 5 dateFormatter.dateFormat = "yyyy-MM-dd" 6 7 return dateFormatter.date(from: date) 8 } 9 10 func stringFromDate(_ date: Date) -> String { 11 let dateFormatter = DateFormatter() 12 dateFormatter.dateFormat = "yyyy-MM-dd" 13 14 return dateFormatter.string(from: date) 15 }
1 import UIKit 2 3 var todos: [ToDoItem] = [] 4 5 class ViewController: UIViewController { 6 7 @IBOutlet weak var todoTableView: UITableView! 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 // Do any additional setup after loading the view, typically from a nib. 12 13 navigationItem.leftBarButtonItem = editButtonItem 14 15 todos = [ToDoItem(id: "1", image: "child-selected", title: "Go to Disney", date: dateFromString("2014-10-20")!), 16 ToDoItem(id: "2", image: "shopping-cart-selected", title: "Cicso shopping", date: dateFromString("2014-10-28")!), 17 ToDoItem(id: "3", image: "phone-selected", title: "Phone to Jobs", date: dateFromString("2014-10-30")!), 18 ToDoItem(id: "4", image: "travel-selected", title: "Plan to Europe", date: dateFromString("2014-10-31")!)] 19 } 20 21 override func viewWillAppear(_ animated: Bool) { 22 super.viewWillAppear(animated) 23 todoTableView.reloadData() 24 } 25 26 // MARK - helper func 27 func setMessageLabel(_ messageLabel: UILabel, frame: CGRect, text: String, textColor: UIColor, numberOfLines: Int, textAlignment: NSTextAlignment, font: UIFont) { 28 messageLabel.frame = frame 29 messageLabel.text = text 30 messageLabel.textColor = textColor 31 messageLabel.numberOfLines = numberOfLines 32 messageLabel.textAlignment = textAlignment 33 messageLabel.font = font 34 messageLabel.sizeToFit() 35 } 36 37 func setCellWithTodoItem(_ cell: UITableViewCell, todo: ToDoItem) { 38 let imageView: UIImageView = cell.viewWithTag(11) as! UIImageView 39 let titleLabel: UILabel = cell.viewWithTag(12) as! UILabel 40 let dateLabel: UILabel = cell.viewWithTag(13) as! UILabel 41 42 imageView.image = UIImage.init(named: todo.image) 43 titleLabel.text = todo.title 44 dateLabel.text = stringFromDate(todo.date) 45 } 46 47 override func didReceiveMemoryWarning() { 48 super.didReceiveMemoryWarning() 49 // Dispose of any resources that can be recreated. 50 } 51 52 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 53 if segue.identifier == "editTodo" { 54 let vc = segue.destination as! DetailViewController 55 let indexPath = todoTableView.indexPathForSelectedRow 56 if let indexPath = indexPath { 57 vc.todo = todos[(indexPath as NSIndexPath).row] 58 } 59 } 60 } 61 62 } 63 64 extension ViewController: UITableViewDataSource { 65 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 66 if todos.count != 0 { 67 return todos.count 68 } else { 69 let messageLabel: UILabel = UILabel() 70 71 setMessageLabel(messageLabel, frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height), text: "No data is currently available.", textColor: UIColor.black, numberOfLines: 0, textAlignment: NSTextAlignment.center, font: UIFont(name: "Palatino-Italic", size: 20)!) 72 73 self.todoTableView.backgroundView = messageLabel 74 self.todoTableView.separatorStyle = UITableViewCellSeparatorStyle.none 75 76 return 0 77 } 78 } 79 80 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 81 let cellIdentifier: String = "todoCell" 82 let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) 83 84 setCellWithTodoItem(cell, todo: todos[(indexPath as NSIndexPath).row]) 85 86 return cell 87 } 88 } 89 90 extension ViewController: UITableViewDelegate { 91 // Edit Mode 92 override func setEditing(_ editing: Bool, animated: Bool) { 93 super.setEditing(editing, animated: animated) 94 todoTableView.setEditing(editing, animated: true) 95 } 96 97 // Delete the cell 98 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 99 if editingStyle == UITableViewCellEditingStyle.delete { 100 todos.remove(at: (indexPath as NSIndexPath).row) 101 todoTableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 102 } 103 } 104 105 // Move the cell 106 func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { 107 return self.isEditing 108 } 109 110 func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { 111 let todo = todos.remove(at: (sourceIndexPath as NSIndexPath).row) 112 todos.insert(todo, at: (destinationIndexPath as NSIndexPath).row) 113 } 114 }
1 import UIKit 2 3 class DetailViewController: UIViewController { 4 5 @IBOutlet weak var childButton: UIButton! 6 @IBOutlet weak var phonebutton: UIButton! 7 @IBOutlet weak var shoppingCartButton: UIButton! 8 @IBOutlet weak var travelButton: UIButton! 9 @IBOutlet weak var todoTitleLabel: UITextField! 10 @IBOutlet weak var todoDatePicker: UIDatePicker! 11 12 var todo: ToDoItem? 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 17 // Do any additional setup after loading the view. 18 19 if let todo = todo { 20 self.title = "Edit Todo" 21 if todo.image == "child-selected" { 22 childButton.isSelected = true 23 } else if todo.image == "phone-selected" { 24 phonebutton.isSelected = true 25 } else if todo.image == "shopping-cart-selected" { 26 shoppingCartButton.isSelected = true 27 } else if todo.image == "travel-selected" { 28 travelButton.isSelected = true 29 } 30 31 todoTitleLabel.text = todo.title 32 todoDatePicker.setDate(todo.date, animated: false) 33 } else { 34 title = "New Todo" 35 childButton.isSelected = true 36 } 37 } 38 39 // MARK: type select 40 @IBAction func selectChild(_ sender: AnyObject) { 41 resetButtons() 42 childButton.isSelected = true 43 } 44 45 @IBAction func selectPhone(_ sender: AnyObject) { 46 resetButtons() 47 phonebutton.isSelected = true 48 } 49 50 @IBAction func selectShoppingCart(_ sender: AnyObject) { 51 resetButtons() 52 shoppingCartButton.isSelected = true 53 } 54 55 @IBAction func selectTravel(_ sender: AnyObject) { 56 resetButtons() 57 travelButton.isSelected = true 58 } 59 60 func resetButtons() { 61 childButton.isSelected = false 62 phonebutton.isSelected = false 63 shoppingCartButton.isSelected = false 64 travelButton.isSelected = false 65 } 66 67 // MARK: create or edit a new todo 68 @IBAction func tapDone(_ sender: AnyObject) { 69 var image = "" 70 71 if childButton.isSelected { 72 image = "child-selected" 73 } else if phonebutton.isSelected { 74 image = "phone-selected" 75 } else if shoppingCartButton.isSelected { 76 image = "shopping-cart-selected" 77 } else if travelButton.isSelected { 78 image = "travel-selected" 79 } 80 81 if let todo = todo { 82 todo.image = image 83 todo.title = todoTitleLabel.text! 84 todo.date = todoDatePicker.date 85 } else { 86 let uuid = UUID().uuidString 87 todo = ToDoItem(id: uuid, image: image, title: todoTitleLabel.text!, date: todoDatePicker.date) 88 todos.append(todo!) 89 } 90 91 let _ = navigationController?.popToRootViewController(animated: true) 92 } 93 94 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 95 super.touchesBegan(touches, with: event) 96 view.endEditing(true) 97 } 98 99 override func didReceiveMemoryWarning() { 100 super.didReceiveMemoryWarning() 101 // Dispose of any resources that can be recreated. 102 } 103 104 /* 105 // MARK: - Navigation 106 107 // In a storyboard-based application, you will often want to do a little preparation before navigation 108 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 109 // Get the new view controller using segue.destinationViewController. 110 // Pass the selected object to the new view controller. 111 } 112 */ 113 114 }