This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func multipleDelete() { | |
| // 存放刪除的 row | |
| var deleteRows:[Int] = [] | |
| // 透過迴圈來遍歷整個 listTableView 的 cell | |
| for row in 0...myTask.count - 1 { | |
| if let cell = listTableView.cellForRow(at: [0,row]) { | |
| // 如果 cell 被選擇了,就加入我們刪除的陣列中 | |
| if cell.isSelected { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @IBAction func changeDeleteMode(_ sender: Any) { | |
| // 遍歷 listTableView 將所有被選取的 cell 取消選取 | |
| for row in 0...myTask.count - 1{ | |
| listTableView.cellForRow(at: [0,row])?.isSelected = false | |
| } | |
| // 判斷左上角的 BarButtonItem.title,進行相對的模式切換 | |
| if self.navigationItem.leftBarButtonItem?.title == "Single" { | |
| self.navigationItem.leftBarButtonItem?.title = "Multiple" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @IBAction func addTask(_ sender: Any) { | |
| // Check taskTextField text | |
| if taskTextField.text == "" { | |
| showAlert() | |
| return | |
| } | |
| myTask.append(taskTextField.text!) | |
| listTableView.insertRows(at: [[0,myTask.count - 1]], with: UITableViewRowAnimation.right) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { | |
| switch editingStyle { | |
| case UITableViewCellEditingStyle.delete: | |
| myTask.remove(at: indexPath.row) | |
| listTableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade) | |
| case UITableViewCellEditingStyle.insert: | |
| if let isSelected = tableView.cellForRow(at: indexPath)?.isSelected { | |
| tableView.cellForRow(at: indexPath)?.isSelected = !isSelected | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override func setEditing(_ editing: Bool, animated: Bool) { | |
| super.setEditing(editing, animated: animated) | |
| listTableView.setEditing(editing, animated: true) | |
| if deleteMode == UITableViewCellEditingStyle.insert { | |
| multipleDelete() | |
| saveData() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| myTableView.dataSource = self | |
| myTableView.delegate = self | |
| refreshControl = UIRefreshControl() | |
| myTableView.addSubview(refreshControl) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func loadData(){ | |
| // 這邊我們用一個延遲讀取的方法,來模擬網路延遲效果(延遲3秒) | |
| DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) { | |
| // 停止 refreshControl 動畫 | |
| self.refreshControl.endRefreshing() | |
| // 新建5筆假資料 | |
| for _ in 1...5 { | |
| self.data.append(self.data.count + 1) | |
| self.myTableView.insertRows(at: [[0,self.data.count - 1]], with: UITableViewRowAnimation.fade) |
OlderNewer