美文网首页IOS
UITableView的实现滑动删除

UITableView的实现滑动删除

作者: meetweb | 来源:发表于2017-12-08 11:40 被阅读0次

    本人参考了其他文章,但是很多版本都是旧的,出现的问题是一直无法实现侧滑后“删除”按钮的出现。

    效果图

    //

    //  UITableViewController.swift

    //  FunBoardTest

    //

    //  Created by meetweb on 2017/12/8.

     //  Copyright © 2017年 meetweb. All rights reserved.

    //

    import UIKit

    class UIMyTableViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{

    var dataList:[String]?

    var tableView:UITableView?

    override func loadView() {

    super.loadView()

    }

    override func viewDidLoad() {

    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.

    self.dataList = ["数据1", "数据2", "数据3", "数据4", "数据5", "数据6", "数据7", "数据8", "数据9", "数据10", "数据11", "数据12", "数据13", "数据14"];

    self.tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.plain)

    self.tableView!.dataSource = self;

    self.tableView!.delegate = self;

    self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")

    self.view.addSubview(tableView!)        // Do any additional setup after loading the view.

    }

    //返回有多少个分组

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1

    }

    //每个分组对应的cell个数

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return dataList!.count

    }

    //创建Cell

    //    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    //        let identifier:String = "Cell"

    //        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath as IndexPath) as UITableViewCell

    //        cell.textLabel!.text = self.dataList![indexPath.row]

    //        return cell;

    //    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let identifier:String = "Cell"

    let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath as IndexPath) as UITableViewCell

    cell.textLabel!.text = self.dataList![indexPath.row]

    return cell;

    }

    //处理选中事件

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    self.tableView!.deselectRow(at: indexPath as IndexPath, animated: true)

    print("选中的Cell 为\(self.dataList![indexPath.row])")

    }

    //返回编辑类型,滑动删除

    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {

    return UITableViewCellEditingStyle.delete

    }

    //在这里修改删除按钮的文字

    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {

    return "删除"

    }

    //点击删除按钮的响应方法,在这里处理删除的逻辑

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == UITableViewCellEditingStyle.delete {

    self.dataList!.remove(at: indexPath.row)

    self.tableView!.deleteRows(at: [indexPath as IndexPath], with: UITableViewRowAnimation.fade)

    }

    }

    override func didReceiveMemoryWarning() {

    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.

    }

    /*

    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    // Get the new view controller using segue.destinationViewController.

    // Pass the selected object to the new view controller.

    }

    */

    }

    实际过程中大家留意一下这个方法

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) 

    如果这个方法出现错误,删除按钮是无法显示的。程序没有报任何的错误

    相关文章

      网友评论

        本文标题:UITableView的实现滑动删除

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