IOS UITableview右滑多个按钮Swift实现

今天在简书上看到一个类似的文章,用的是oc方式实现,然后自己无聊用Swift方法实现下,也算是练习一下。

代码如下

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
    var tableview:UITableView = UITableView();//建立tableview
    var dataArr:NSMutableArray = [1,2,3,4,5,6,7,8];//设置数据源
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        tableview.bounds = UIScreen.mainScreen().bounds;//设置tablview的大小
        tableview.center = CGPointMake(UIScreen.mainScreen().bounds.maxX/2, UIScreen.mainScreen().bounds.maxY/2)//设置tableview的中心
        tableview.backgroundColor = UIColor.whiteColor();
        tableview.delegate=self;
        tableview.dataSource=self;
        self.view.addSubview(tableview);
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1;
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr.count;
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let Idnetfier = "cell"
        let cell:UITableViewCell = UITableViewCell(style: .Default, reuseIdentifier: Idnetfier)
        cell.textLabel?.text = "\(dataArr[indexPath.row])I am a cell";
        return cell;
    }
    //设置动作按钮的函数
    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
        //添加删除按钮
        let deleteRowAction:UITableViewRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Destructive, title: "删除", handler: {
            (action:UITableViewRowAction,index:NSIndexPath) in
            //先从数据源那里删除数据
            self.dataArr.removeObjectAtIndex(indexPath.row);
            //然后在把tableview上的指定行删除
            self.tableview.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic);
            
        })
        let insertRowAction:UITableViewRowAction = UITableViewRowAction(style:.Default, title: "置顶", handler: {
            (action:UITableViewRowAction,index:NSIndexPath) in
            //把数据源地一行的数据和当前点击行的数据交换
            self.dataArr.exchangeObjectAtIndex(0, withObjectAtIndex: indexPath.row);
            //获取tableview中第一行的indexpath
            let firstIndex:NSIndexPath = NSIndexPath(forRow: 0, inSection: indexPath.section)
            //通过第一行的index和当前点击行的index来进行行的交换
            self.tableview.moveRowAtIndexPath(indexPath, toIndexPath: firstIndex);
        })
        let moreRowAction:UITableViewRowAction = UITableViewRowAction(style:.Default, title: "重置", handler: {
            (action:UITableViewRowAction,index:NSIndexPath) in
            //重新加载tablview
            self.tableview.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Bottom)
        })
        let actions = [deleteRowAction,insertRowAction,moreRowAction];
        return actions;
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,540评论 4 61
  • 如果你的孩子各方面条件都很不错,但一直没有找女朋友,你又着急抱孙子,你会怎么做,或者是遇见这样条件优秀的人,你会怎...
    爱丫的电影美食和育儿阅读 3,375评论 1 1
  • 正则表达式是用来简洁表达一组字符串的表达式 使用正则表达式的优势是什么?简洁一行胜千言 一行就是特征(模式) 无穷...
    六尺帐篷阅读 3,757评论 0 11
  • 这是本教你如何交朋友,谈恋爱;教你如何迅速进入社会,轻松立足社会,在职场生存;教你如何受人尊重,顺利说服他人...
    噤若寒蝉阅读 4,438评论 0 1

友情链接更多精彩内容