美文网首页
tableview 左滑删除或者编辑

tableview 左滑删除或者编辑

作者: iYeso | 来源:发表于2017-04-12 16:45 被阅读129次

1: 左滑出现一个按钮

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

  let tableView:UITableView = {
    let tableView = UITableView()
    tableView.frame = UIScreen.main.bounds
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    tableView.rowHeight = 60
    return tableView
  }()

  var array:[String] = ["One", "two", "three", "four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"]

  override func viewDidLoad() {
    super.viewDidLoad()
    self.view.addSubview(tableView)
  }


}

extension ViewController{
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.array.count
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = self.array[indexPath.row]
    return cell
  }

// 添加编辑模式
  func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
  }

// 左侧的左侧出现的字符串
  func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
    return "删掉哈"
  }

//  *  只要实现了这个方法,左滑出现Delete按钮的功能就有了
// *  点击了“左滑出现的Delete按钮”会调用这个方法
  func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    self.array.remove(at: indexPath.row)
    tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade)
  }

}

二: 左滑出现多个按钮选项

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

  let tableView:UITableView = {
    let tableView = UITableView()
    tableView.frame = UIScreen.main.bounds
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    tableView.rowHeight = 60
    return tableView
  }()

  var array:[String] = ["One", "two", "three", "four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"]

  override func viewDidLoad() {
    super.viewDidLoad()
    self.view.addSubview(tableView)
    tableView.delegate = self
    tableView.dataSource = self
  }


}

extension ViewController{
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.array.count
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = self.array[indexPath.row]
    return cell
  }

  func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
  }

}

// MARK: 处理tableView的代理
extension ViewController{

  func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let actionOne = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "增加") { (action:UITableViewRowAction, index:IndexPath) in

      // 增加

      self.array.insert("增加+++", at: indexPath.row)
      self.tableView.insertRows(at: [indexPath], with: UITableViewRowAnimation.fade)
      tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.none)
    }

    actionOne.backgroundColor = UIColor.orange


    let actionTwo = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "删除") { (action:UITableViewRowAction, index:IndexPath) in
      print("点击删除")

      self.array.remove(at: indexPath.row)
      self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade)
    }


    actionTwo.backgroundEffect = UIBlurEffect(style: UIBlurEffectStyle.regular)
    return [actionOne,  actionTwo]
  }
}

相关文章

网友评论

      本文标题:tableview 左滑删除或者编辑

      本文链接:https://www.haomeiwen.com/subject/mnbmattx.html