小而美的Swift&iOS教程03-BasicListEdit

BasicViewEdit.gif

这个教程会涉及如下内容


  • 给TableView添加下拉刷新功能
  • 给TableView添加编辑编辑功能
  • 给TableView添加删除功能

UIRefreshControl下拉刷新



var refreshControl: UIRefrshControl! 
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        refreshControl = UIRefreshControl()
        refreshControl.addTarget(self, action: #selector(refreshTable), for: .valueChanged)//添加响应目标函数
        animalsTableView.refreshControl = refreshControl
    }

定义响应函数refreshTable()

func refreshTable(){
        self.animalList = cloudAnimalList
        animalsTableView.reloadData()//重新加载列表
        refreshControl.endRefreshing() //停止刷新
    }

实现编辑功能


给navigationbar左上角添加edit按钮
如果你是直接使用TableViewController,可以直接使用以下语句完成编辑功能

self.navigationItem.leftBarButtonItem = self.editButtonItem

如果TableView是在ViewController基础上实现的话
需要在左上角edit按钮的action方法中添加如下代码

@IBAction func touchButtonItem(_ sender: UIBarButtonItem) {
        if let title = sender.title{
            switch title{
                case "Edit":
                    animalsTableView.setEditing(true, animated: true)
                    sender.title = "Done"
                case "Done":
                    animalsTableView.setEditing(false, animated: true)
                    sender.title = "Edit"
                default: break
            }
        
        }
    }
实现删除功能

在DataSource中实现以下方法设定每个cell在isEdit=true时所显示的状态
状态有两种可选形式 一种是.delete另一种是.insert

    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
        return UITableViewCellEditingStyle.delete
    }

同时还要在Delegete中实现以下方法来响应点击


    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete{
            animalList.remove(at: indexPath.row)
            animalsTableView.deleteRows(at: [indexPath], with: .fade)
        }else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }
    }
实现编辑功能

在DataSource中实现如下方法,使得数组中的元素和tableview一致

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        animalList.insert(animalList.remove(at: sourceIndexPath.row), at: destinationIndexPath.row)
    }

这样我们就实现了刷新、删除、编辑的功能。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前两天在路上看到十点读书会发布的这个活动就想到在长假的最后一天来写一写。我的生活在我看来也许还不能用疲惫来形容,我...
    职场小能手Sara阅读 2,241评论 0 1
  • 根据上周的实际情况,工作加照顾小孩,原设计的投资计划主题实际难以实现。主题一利用上班空余时间看书学习内部审计,时间...
    请叫我静静阅读 2,241评论 0 0
  • 大L认识她,已经快有五年了,五年前QQ刚有了点赞功能、五年前,大L才高二。两个无聊的人,或许因为夏天快要结束...
    没有勇气的流浪者阅读 1,508评论 0 0
  • 姥姥:靓靓啊,你背单词就背单词,为什么要对着镜子呢? 靓靓(娇羞):人家就是想看看自~己~的~美~貌~~。 姥姥:...
    数字姥姥阅读 1,898评论 0 1
  • 文主出生在农村,而农村向来就有只有读书才是唯一出路的“传统”,因此自打七岁被送入学校就受到了来自父母高度的关注,...
    学法的法盲人士阅读 4,243评论 1 2