本文共 6097 字,大约阅读时间需要 20 分钟。
let cell = tableView.dequeueReusableCell(withIdentifier: "todo", for: indexPath) as! TodosCell
//重用celloverride func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { todos[indexPath.row].isChecked = !todos[indexPath.row].isChecked // 修改数据中是否被惦记的属性 let cell = tableView.cellForRow(at: indexPath) as! TodosCell // 获取的点击的那个cell cell.checkMark.text = todos[indexPath.row].isChecked ?"√":"" //改变视图 tableView.deselectRow(at: indexPath, animated: true)// 取消点击 }
// 绑定委托者 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "addTodo"{ let vc = segue.destination as! AddTodoVC vc.delegate = self } //第一响应: todoInputTX.becomeFirstResponder() // sava点击函数 @IBAction func saveTodo(_ sender: Any) { if let name = todoInputTX.text{ delegate?.addTodo(name: name) } navigationController?.popViewController(animated: true) } //实现委托函数 func addTodo(name: String) { todos.append(Todo(todo: name, isChecked: false))//添加数据到model里面。 let indexPath = IndexPath(row: todos.count-1, section: 0) tableView.insertRows(at: [indexPath], with: .automatic)//添加一个新的todo Rows }
注意事项: 如果界面编辑完成之后,运行之后的界面和预期不一致,检查一下操作代码,是否修改界面,也就是先使用storyboard渲染界面,然后再用代码修改界面。
// 另一个segue路径,传值过去else if segue.identifier == "editTodo"{ let vc = segue.destination as! AddTodoVC vc.delegate = self//代理绑定 let cell = sender as! TodosCell let indexPath = tableView.indexPath(for: cell)! editRow = indexPath.row vc.editName = todos[editRow!].todo//传递参数 }// 实现edit 代理函数:func editTodo(name: String) { todos[editRow!].todo = name// let indexPath = IndexPath(row: editRow!, section: 0) let cell = tableView.cellForRow(at: indexPath) as! TodosCell cell.todoLabel.text = todos[editRow!].todo }//如果是从 edit 跳转过来,修改title,点击save执行另外一个代理: if editName != nil{ navigationItem.title = "Edit Todo" } if let name = todoInputTX.text{ if editName != nil { delegate?.editTodo(name: name)//走这个代理 }else{ delegate?.addTodo(name: name) } }
//删除某一个row 实现左滑动删除 override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { todos.remove(at: indexPath.row)//从模型中删除数据 // Delete the row from the data source tableView.deleteRows(at: [indexPath], with: .fade) } else if editingStyle == .insert { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view }//修改删除时候显示的文字: override func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? { return "删除" }// 在代码中添加 editing btn,并且修改显示的title:self.navigationItem.leftBarButtonItem = self.editButtonItem editButtonItem.title = "编辑" override func setEditing(_ editing: Bool, animated: Bool) { super.setEditing(editing, animated: animated) editButtonItem.title = isEditing ? "完成" : "编辑" }
// 批量删除: @IBAction func deleteRows(_ sender: Any) { if let indexPaths = tableView.indexPathsForSelectedRows{ for indexPath in indexPaths{ todos.remove(at: indexPath.row) }//这个方法删除数据不安全 tableView.beginUpdates()//提高更新性能 tableView.deleteRows(at: indexPaths, with: .automatic)// 批量删除rows tableView.endUpdates()//提高更新性能 } }// 移动rows override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) { // 模型移动数据 let from = todos.remove(at: fromIndexPath.row) todos.insert(from, at: to.row) // view 进行移动 tableView.moveRow(at: fromIndexPath, to: to) }
// 编码,存储到本地plist里面: func saveData(){ do{ //编码 let data = try JSONEncoder().encode(todos) //存到本地,key为”todos“ UserDefaults.standard.set(data, forKey: "todos") }catch{ print(error) } }// 从本地存储中取数据 if let data = UserDefaults.standard.data(forKey: "todos"){ do { // 数据解码成类。 todos = try JSONDecoder().decode([Todo].self, from: data) } catch{ print(error) } }//定义空数组,空字典的另一种写法:var todos = [Todo]()var dic = [String:String]()
转载地址:http://gcywi.baihongyu.com/