UITableViewCell,在滑动时显示删除按钮
  如何在UITableViewCell上滑动时显示删除按钮?  事件永远不会被提出,删除按钮也不会出现。 
  在(-viewDidLoad or in storyboard)启动过程中(-viewDidLoad or in storyboard)请执行以下操作: 
self.tableView.allowsMultipleSelectionDuringEditing = NO;
  重写以支持表视图的条件编辑。  这只需要实现,如果你将要返回NO一些项目。  默认情况下,所有项目都是可编辑的。 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
    }    
}
这个答案已经更新到Swift 3
  我总是认为有一个非常简单,独立的例子是很好的,这样在我学习一项新任务时就不会有任何假设。  这个答案是用于删除UITableView行。  该项目执行如下: 

该项目基于Swift的UITableView示例。
添加代码
创建一个新项目并用以下代码替换ViewController.swift代码。
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    // These strings will be the data for the table view cells
    var animals: [String] = ["Horse", "Cow", "Camel", "Pig", "Sheep", "Goat"]
    let cellReuseIdentifier = "cell"
    @IBOutlet var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        // It is possible to do the following three things in the Interface Builder
        // rather than in code if you prefer.
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
        tableView.delegate = self
        tableView.dataSource = self
    }
    // number of rows in table view
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.animals.count
    }
    // create a cell for each table view row
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
        cell.textLabel?.text = self.animals[indexPath.row]
        return cell
    }
    // method to run when table view cell is tapped
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("You tapped cell number (indexPath.row).")
    }
    // this method handles row deletion
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // remove the item from the data model
            animals.remove(at: indexPath.row)
            // delete the table view row
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Not used in our example, but if you were adding a new row, this is where you would do it.
        }
    }
}
上面的代码中的单个关键方法是允许行删除的最后一个。 这里再次强调:
// this method handles row deletion
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        // remove the item from the data model
        animals.remove(at: indexPath.row)
        // delete the table view row
        tableView.deleteRows(at: [indexPath], with: .fade)
    } else if editingStyle == .insert {
        // Not used in our example, but if you were adding a new row, this is where you would do it.
    }
}
故事板
  在故事板中的View Controller中添加一个UITableView 。  使用自动布局将表视图的四边固定到View Controller的边缘。  控制从故事板中的表格视图拖动到@IBOutlet var tableView: UITableView!  在代码中的行。 
成品
就这样。 您应该可以立即运行您的应用程序,并通过向左轻扫并点击“删除”来删除行。
变化
更改“删除”按钮文本

添加以下方法:
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
    return "Erase"
}
自定义按钮操作

添加以下方法。
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    // action one
    let editAction = UITableViewRowAction(style: .default, title: "Edit", handler: { (action, indexPath) in
        print("Edit tapped")
    })
    editAction.backgroundColor = UIColor.blue
    // action two
    let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, indexPath) in
        print("Delete tapped")
    })
    deleteAction.backgroundColor = UIColor.red
    return [editAction, deleteAction]
}
请注意,这仅适用于iOS 8.请参阅此答案以获取更多详细信息。
针对iOS 11更新
可以使用添加到iOS 11中的UITableViewDelegate API的方法来引导或拖尾单元格。
func tableView(_ tableView: UITableView,
                leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
 {
     let editAction = UIContextualAction(style: .normal, title:  "Edit", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
             success(true)
         })
editAction.backgroundColor = .blue
         return UISwipeActionsConfiguration(actions: [editAction])
 }
 func tableView(_ tableView: UITableView,
                trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
 {
     let deleteAction = UIContextualAction(style: .normal, title:  "Delete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
         success(true)
     })
     deleteAction.backgroundColor = .red
     return UISwipeActionsConfiguration(actions: [deleteAction])
 }
进一步阅读
此代码显示如何实现删除。
#pragma mark - UITableViewDataSource
// Swipe to delete.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_chats removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}
或者,在您的初始化覆盖中,添加下面的行以显示“编辑”按钮项:
self.navigationItem.leftBarButtonItem = self.editButtonItem;
上一篇: UITableViewCell, show delete button on swipe
下一篇: Remove all subviews?
