1.實作TableViewCell左滑顯示刪除按鈕
/**
* 實作TableView方法,自動出現左滑功能
**/
func tableView(_ tableView: UITableView,
commit editingStyle: UITableViewCellEditingStyle,
forRowAt indexPath: IndexPath)
{
let select:Int = indexPath.row
//=========
// 操作
//=========
tableView.reloadData() // 更新tableView
}
/**
* 自訂delete的文字為刪除
*/
func tableView(_ tableView: UITableView,
titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath)
-> String?
{
return "刪除"
}
2.實作TableViewCell左滑顯示多個按鈕
/**
* 實作TableView方法,自動出現左滑功能,並進入編輯模式
* 編輯模式 => tableView.isEditing = true
**/
func tableView(_ tableView: UITableView,
commit editingStyle: UITableViewCellEditingStyle,
forRowAt indexPath: IndexPath) {
}
/**
* 實作TableView回傳Action Array的方法,來自定左滑後顯示的按鈕及動作
**/
func tableView(_ tableView: UITableView,
editActionsForRowAt indexPath: IndexPath)
-> [UITableViewRowAction]?
{
var actionArr:Array<UITableViewRowAction> = [UITableViewRowAction]()
// 建立編輯按鈕
let actionEdit:UITableViewRowAction =
UITableViewRowAction(style: UITableViewRowActionStyle.normal,
title: "編輯") { (action, indexPath) in
let select:Int = indexPath.row
//=========
// 編輯操作
//=========
tableView.isEditing = false; // 退出編輯模式
tableView.reloadData() // 更新tableView
}
// 建立刪除按鈕
let actionDelete:UITableViewRowAction =
UITableViewRowAction(style: UITableViewRowActionStyle.default,
title: "刪除") { (action, indexPath) in
let select:Int = indexPath.row
//=========
// 刪除操作
//=========
tableView.isEditing = false; // 退出編輯模式
tableView.reloadData() // 更新tableView }
// 將按鈕動作加入Array,並回傳
actionArr.append(actionEdit)
actionArr.append(actionDelete)
return actionArr;
}