给 Homepwner 添加新的功能,使 UITableView 可以响应用户操作,包括添加 、删除和移动表格的行。
编辑模式
- UITableView 有 一 个名为
editing 属性
,如果将 editing 属性设置为 true, UITableView 就会进入编辑模式 。 - 在编辑模式下,用户可以管理 UITableView 中的表格的行 , 例如之前提到的添加、删除和移动操作。但是编辑模式没有提供修改行的内容的功能。
- 首先需要更新界面 ,可以让用户将 UITableView 对象设置为编辑模式 。为 UITableView 对象的
header view (表头视图)
增加 一 个按钮,然后通过点击按钮使 UITableView 对象进入或退出编辑模式 。表头视图是显示在 UITableView 对象表格上方的特定视图,适合放置针对某个组或者整张表格的标题和控件 。 表头视图可以是任意的 UIView 对象 。
- 要编辑界面需要“edit”和“add”两个Button,所以需要再Controler里初步定义两个UIButton类型的插座变量方法。
- 定义完成后,拖两个UIButton控件到表头,然后进行约束。
- 约束完毕后,将Controler定义的Button与故事板里的UIButton关联起来。
- 接下来,需要给两个Button编写实现具体功能的方法。代码如下:
Edit
if isEditing { //如果当前处于编辑模式中
sender.setTitle("Edit", for: .normal) //修改按钮的文字来提示用户
setEditing(false, animated: true) //退出编辑模式
} else {
sender.setTitle("Done", for: .normal)
setEditing(true, animated: true) ///进入编辑模式
}
}
Add
@IBAction func addNewItem(_ sender: UIButton){
// let lastRow = tableView.numberOfRows(inSection: 0) //创建一个第0组最后一行的 indexPath 对象。
// let indexPath = IndexPath(row: lastRow, section: 0)
// tableView.insertRows(at: [indexPath], with: .automatic)
//这样只是在 tableview 中插入了新的行。并没有在itemStore中增加相应的数据源。
/*
点击添加按钮,然后应用就崩溃了 。在添加新行之后, UITableView有6行了(添加之前是5行) 。
当 UITableView 向数据源询问有多少行时, ItemsViewController 查询了 ItemStore 然后返回了5行 。
UITableView 不能处理内部不一致异常,并抛出了错误 。
最终是 UITableView 的数据源决定了 UITableView 应该显示多少行 。
*/
/*
必须确保 UITableView 与它的数据源的行数是一致的 。 ItemStore 中添加一个新的 Item。
*/
let newItem = itemStore.createItem() //创建一个新的 item 对象并加入 itemStore 中。增加的数据源。
if let index = itemStore.allItems.firstIndex(of: newItem){ //找出 item 对象在数组中的位置。
let indexPath = IndexPath(row: index, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic)
}
}
- 点击Edit按钮进入编辑模式,但是目前还不能删除行和排序的操作。同样需要定义相关的方法来实现功能。
- 在 UITableView 删除行之前,会调用数据源的一个方法,等待确认后才会执行删除操作。
- 删除行需要做两件事:删除 UITableView 的行和删除 ItemStore 中相应的 Item。要完成这件事, ItemStore 需要知道如何删除 Item 。
- 在 ItemStore. swift 中 ,实现一个新方法来删除指定的 Item 。(ItemStore 才是存储数据的地方 , ItemsViewController 是 UITableView 的 dataSource 。)
func removeItem(_ item: Item){
if let index = allItems.firstIndex(of: item){
allItems.remove(at: index)
}
}
- 现在实现 UITableViewDataSource 的
tableView(_: commit: forRow: )
方法。在 ItemsViewController . swift 中实现这个方法 , 在方法中从 ItemStore 删除正确的 Item, 并且调用 UITableView 的deleteRows(at:with:)
来确认删除完成,代码如下:
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{ //如杲 TableView 确认了删除命令
let item = itemStore.allItems[indexPath.row]
itemStore.removeItem(item) //从 itemStore 中删除 item
tableView.deleteRows(at: [indexPath], with: .automatic)
//同时从 TableView 中删除行,行消失的时候制作一个动画
}
}
- 改变 UITableView 行的顺序 ,需要使用 UITableViewDataSource 协议的另 一个方法
tableView(_:moveRowAt:to: )
。在实现这个方法之前 ,需要在 ItemStore 中实现一个方法来改变 allltems 数组元素的顺序。在 Items to re.swift 中实现这个新方法,代码如下:
func moveItem(from fromIndex: Int, to toIndex: Int){
if fromIndex == toIndex{
return
}
let movedItem = allItems[fromIndex] //获取需要移动的对象
allItems.remove(at: fromIndex) //从数组中删除
allItems.insert(movedItem, at: toIndex) //在新的位置插入
}
- 在 ItemsViewController. swift 中 ,实现 tableView(_:moveRowAt:to:) 方法来更新 ItemStore 中的数据,代码如下 :
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
itemStore.moveItem(from: sourceIndexPath.row, to: destinationIndexPath.row)
}
- 实现 tableView(: moveRowAt: to: )之后换位控件就出现了。 UITableView 在运行时会查询数据源是否实现了 tableView(: moveRowAt: to:) ,如果已实现,就会在进入编辑模式后显示换位控件。
显示弹窗
当用户执行一个重要操作时,弹窗可以用来提醒用户,让用户有机会取消操作。可以创建一 个UIAlertController 对象
, 然后选择 一 个样式来显示弹窗。
- 弹窗支持两种样式:
UIAlertControllerStyle.actionSheet
和UIAlertControllerStyle.alert
。
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{ //如杲 TableView 确认了删除命令
let item = itemStore.allItems[indexPath.row]
/*
当发现用户要删除 Item 时,使用合适的标题、提示和样式创建 UIAlertController 对象 , 可以提示用户将要发生的操作。这里把样式设置为 .actionSheet 。
*/
let title = "Delete \(item.name)?"
let message = "Are you sure yao want to delete this item?"
let ac = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
/*
弹窗中展示的动作是 UIAlertAction 对象,所有样式的弹窗都可以添加多个动作。通过 UIAlertController 的 addAction(_:) 方法可以添加动作。
*/
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
ac.addAction(cancelAction)
//第一个动作使用 “Cancel” 标题和 .cancel样式创建。 .cancel样式创建的动作会显示标准的蓝色字体。这个动作可以让用户取消删除 Item 。
//handler 参数可以传递一个闭包,闭包会在动作触发时被调用 。这里不需要任何其他操作,因此传入了 nil 。
let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: {(action) -> Void in
self.itemStore.removeItem(item)
self.tableView.deleteRows(at: [indexPath], with: .automatic)
})
//第二个动作使用 “Delete”标题和 .destructive 样式创建。 .destructive样式的文字是明亮的红色。
//如果用户选择了这个动作,则 UITableView 中的 Item 就会被删除。删除操作是在 handler 的闭包中完成的。
ac.addAction(deleteAction)
present(ac, animated: true, completion: nil)
//调用 UIViewController 的 present(_: animated: completion:) 方法可以显示全屏的视图。
// if editingStyle == .delete{ //如杲 TableView 确认了删除命令
// let item = itemStore.allItems[indexPath.row]
// itemStore.removeItem(item)
// tableView.deleteRows(at: [indexPath], with: .automatic)
// }
}
}
Simulator Screen Shot - iPhone 11 - 2021-11-27 at 21.53.04.png