Swift-UI多选删除

//

//  ViewController.swift

//  UITableView-MoreDelete

//

//  Created by Qsyx on 16/6/24.

//  Copyright © 2016 Qsyx. All rights reserved.

//


import UIKit


class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    

    //宏定义

    let Width = UIScreen.mainScreen().bounds.size.width

    let Height = UIScreen.mainScreen().bounds.size.height

    

    //带参的宏定义

    func MakeColor(r : CGFloat, g :  CGFloat, b : CGFloat) -> UIColor {

        return UIColor.init(red: r, green: g, blue: b, alpha: 1.0)

    }


    //<1>属性

    //UITableView

    var tableView : UITableView!

    //数据源

    var dataArray : NSMutableArray!

    

    //右按钮

    var allButton : UIBarButtonItem!

    var deleteButton : UIBarButtonItem!

    

    

    //待删除的数据的数组

    var deleteArray : NSMutableArray!

    //待删除的cell的数组

    var cellArray : NSMutableArray!

    

    

    //记录全选按钮的点击状态:是否应该执行全选操作,还是全不选的操作

    var isAll : Bool = true

    

    

    override func viewDidLoad() {

        super.viewDidLoad()

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

        

        self.view.backgroundColor = MakeColor(0.8, g: 0.5, b: 0.3)

        

        self.automaticallyAdjustsScrollViewInsets = false

        

        //<1>创建数据源

        self.creatDataArray()

        

        //<2>创建UITableView

        self.creatTableView()

        

        //<3>创建左右按钮

        self.creatButton()

        

    }

    

    // MARK: - 创建数据源

    func creatDataArray() -> Void {

        //

        //<1>开辟内存空间

        self.dataArray = NSMutableArray()

        self.cellArray = NSMutableArray.init(capacity: 0)

        self.deleteArray = NSMutableArray.init(capacity: 0)

        

        //<2>创建数据源

        for i in 1...30 {

            dataArray.addObject("这是第\(i)数据")

        }

        

    }

    // MARK: - 创建UITableView

    func creatTableView() -> Void {

        //

        self.tableView = UITableView.init(frame: CGRectMake(0, 64, Width, Height - 64), style: UITableViewStyle.Plain)

        self.tableView.delegate = self

        self.tableView.dataSource = self

        self.view.addSubview(self.tableView)

        

        //注册cell

        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "iden")

        

    }

    // MARK: - 创建左右按钮

    func creatButton() -> Void {

        //左按钮

        self.navigationItem.leftBarButtonItem = self.editButtonItem()

        

        //删除按钮

        self.deleteButton = UIBarButtonItem.init(title: "删除", style: UIBarButtonItemStyle.Done, target: self, action: Selector("deleteButtonClick"))

        //全选按钮的创建

        self.allButton = UIBarButtonItem.init(title: "全选", style: UIBarButtonItemStyle.Done, target: self, action: Selector("allButtonClick"))

        

    }

    // MARK: - 编辑按钮对应的方法

    override func setEditing(editing: Bool, animated: Bool) {

        super.setEditing(editing, animated: animated)

        

        //editing参数:false -> true -> false

        //判断editing的状态

        if editing {

            self.navigationItem.rightBarButtonItems = [self.deleteButton, self.allButton]

        }else{

            self.navigationItem.rightBarButtonItems = nil

        }

        

        

        //deleteArray清空

        //cellArray清空

        self.deleteArray.removeAllObjects()

        self.cellArray.removeAllObjects()

        

        

        self.isAll = true

        

        

        //设置UITableView的编辑状态

        //self.tableView.editing  能获取到tableView的编辑状态  默认false

        self.tableView.setEditing(!self.tableView.editing, animated: true)

//        self.tableView.setEditing(editing, animated: true)

        

    }

    

    // MARK: - 一键删除对应的方法

    func deleteButtonClick() -> Void {

        //

        //deleteArray里面的数据从DataArray里面删掉

        self.dataArray.removeObjectsInArray(self.deleteArray as [AnyObject])

        //刷新数据

//        self.tableView.reloadData()

        

        //带有动画效果的删除

        self.tableView.deleteRowsAtIndexPaths(self.cellArray as NSArray as! Array, withRowAnimation: UITableViewRowAnimation.Automatic)

        

        

        //清空之前的元素

        self.deleteArray.removeAllObjects()

        self.cellArray.removeAllObjects()

    }

    

    // MARK: - 全选按钮对应的方法

    func allButtonClick() -> Void {

        

        

        if self.isAll {

            //全选

            //遍历数据源的个数

            for row in 0..<self.dataArray.count {

                //获取所有cell的下标

                let indexPath = NSIndexPath.init(forRow: row, inSection: 0)

                //将所有的cell选中

                self.tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)

            

                //cell的下标放到cellArray里面

                self.cellArray.addObject(indexPath)

            }

            //dataArray里面的所有数据放到deleteArray里面

            self.deleteArray.addObjectsFromArray(self.dataArray as [AnyObject])

            

            //将状态置反

            self.isAll = false

            

        }else{

            //全不选

            for row in 0..<self.dataArray.count {

                let index = NSIndexPath.init(forRow: row, inSection: 0)

                //取消选中

                self.tableView.deselectRowAtIndexPath(index, animated: true)

            }

            

            //将数组置空

            self.deleteArray.removeAllObjects()

            self.cellArray.removeAllObjects()

            //将状态置反

            self.isAll = true

        }

        

        

        

        

    }

    

    // MARK: - 基本的代理方法

    //设置行数

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

        return self.dataArray.count

    }

    //UITableViewCell

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

        //

        let cell = tableView.dequeueReusableCellWithIdentifier("iden", forIndexPath: indexPath)

        

        cell.textLabel?.text = self.dataArray[indexPath.row] as? String

        

        return cell

    }

    

    // MARK: - 多选删除相关的代理方法

    //设置编辑状态

    func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {

//        return UITableViewCellEditingStyle.Delete //单选删除

//        return UITableViewCellEditingStyle.Insert //新增元素

        return UITableViewCellEditingStyle.init(rawValue: UITableViewCellEditingStyle.Delete.rawValue | UITableViewCellEditingStyle.Insert.rawValue)!

    }

    //选中事件:将选中的那一行的数据放到deleteArray

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        //

        //判断

        //tableView处于编辑状态时

        if self.tableView.editing {

            //将选中的那一行的数据放到deleteArray

            

            self.deleteArray.addObject(self.dataArray[indexPath.row])

            //动画效果

            //将选中的cell放倒数组里面  cellArray

            self.cellArray.addObject(indexPath)

            

            

            self.isAll = false

            

            

        }else{

            //当不处于编辑状态时:做其他操作

            tableView.deselectRowAtIndexPath(indexPath, animated: true)

        }

    }

    

    //取消选中:将不再取消的数据从deleteArray里面删除

    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

        //

        if self.tableView.editing {

            //将不再取消的数据从deleteArray里面删除

            self.deleteArray.removeObject(self.dataArray[indexPath.row])

            //将不再选中的cellcellArray里面移除

            self.cellArray.removeObject(indexPath)

            

            self.isAll = true

            

            

        }else{

            print("qwe")

        }

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }



}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值