swift中UITableView的使用(编辑模式)

swift中UITableView的使用(编辑模式),第1张

概述https://github.com/potato512/SYSwiftLearning override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. self.navigationItem.

https://github.com/potato512/SYSwiftLearning



overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        // Do any additional setup after loading the vIEw.                self.navigationItem.Title = "edittable"                // 切换列表的编辑模式        // 方法1 自定义按钮//        let editbutton = UIbutton(type: .Custom)//        editbutton.frame = CGRectMake(0.0,0.0,60.0,40.0)//        editbutton.setTitle("edit",forState: .normal)//        editbutton.setTitlecolor(UIcolor.blackcolor(),forState: .normal)//        editbutton.selected = false//        editbutton.addTarget(self,action: Selector("editClick:"),forControlEvents: .touchUpInsIDe)//        self.navigationItem.rightbarbuttonItem = UIbarbuttonItem(customVIEw: editbutton)        // 方法2 系统按钮        self.navigationItem.rightbarbuttonItem = self.editbuttonItem()                self.setLocalData()        self.setUI()}
// MARK: - 数据func setLocalData(){        self.mainArray = NSMutableArray()                for number in 1...10        {            let numberTmp = random() % 1000 + number            self.mainArray.addobject(String(numberTmp))        }}
// MARK: - 视图func setUI(){        self.maintableVIEw = UItableVIEw(frame: self.vIEw.bounds,style: .Plain)        self.vIEw.addSubvIEw(self.maintableVIEw)        self.maintableVIEw.backgroundcolor = UIcolor.clearcolor()        self.maintableVIEw.delegate = self        self.maintableVIEw.dataSource = self        self.maintableVIEw.autoresizingMask = UIVIEwautoresizing.FlexibleHeight        self.maintableVIEw.tableFooterVIEw = UIVIEw()}
// MARK: - clickfunc editClick(button:UIbutton){        // 进入编辑模式,或退出编辑模式        // 方式1//        self.maintableVIEw.editing = !self.maintableVIEw.editing//        //        button.setTitle((self.maintableVIEw.editing ? "done" : "edit"),forState: .normal)                // 方法2        button.selected = !button.selected        self.maintableVIEw.setEditing(button.selected,animated: true)                button.setTitle((button.selected ? "done" : "edit"),forState: .normal)}
// 进入编辑模式(结合导航栏编辑按钮使用:self.navigationItem.rightbarbuttonItem = self.editbuttonItem())overrIDe func setEditing(editing: Bool,animated: Bool) {        super.setEditing(editing,animated: animated)        self.maintableVIEw.setEditing(editing,animated: true)}
// MARK: - UItableVIEwDataSource,UItableVIEwDelegatefunc numberOfSectionsIntableVIEw(tableVIEw: UItableVIEw) -> Int {        return 1    }        func tableVIEw(tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int {        return self.mainArray.count}    func tableVIEw(tableVIEw: UItableVIEw,cellForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCell {        var cell:UItableVIEwCell! = tableVIEw.dequeueReusableCellWithIDentifIEr("UItableVIEwCell")        if cell == nil        {            cell = UItableVIEwCell(style: .Default,reuseIDentifIEr: "UItableVIEwCell")        }                let text = self.mainArray[indexPath.row] as! String        cell.textLabel!.text = text                return cell}    func tableVIEw(tableVIEw: UItableVIEw,dIDSelectRowAtIndexPath indexPath: NSIndexPath) {        tableVIEw.deselectRowAtIndexPath(indexPath,animated: true)}
// MARK: - cell编辑//    func tableVIEw(tableVIEw: UItableVIEw,editactionsForRowAtIndexPath indexPath: NSIndexPath) -> [UItableVIEwRowAction]? {//        //    }    //    func tableVIEw(tableVIEw: UItableVIEw,canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {//        return true//    }    // MARK: 删除,或插入// cell的编辑样式func tableVIEw(tableVIEw: UItableVIEw,editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UItableVIEwCellEditingStyle {        if indexPath.row == self.mainArray.count - 1        {            // 最后一个时插入            return UItableVIEwCellEditingStyle.Insert        }        else if indexPath.row == 0        {            // 第一个没有编辑模式            return UItableVIEwCellEditingStyle.None        }                // 其他cell为删除的编辑模式(设置tableVIEw的editing属性进行删除 *** 作;或左滑cell进行删除 *** 作)        return UItableVIEwCellEditingStyle.Delete}    // cell的删除编辑样式下按钮标题func tableVIEw(tableVIEw: UItableVIEw,TitleForDeleteConfirmationbuttonForRowAtIndexPath indexPath: NSIndexPath) -> String? {        return "删除"}    // cell的编辑响应func tableVIEw(tableVIEw: UItableVIEw,commitEditingStyle editingStyle: UItableVIEwCellEditingStyle,forRowAtIndexPath indexPath: NSIndexPath) {                if (editingStyle == UItableVIEwCellEditingStyle.Delete)        {            // 删除数据方法1(先删除数据,再重新加载全部数据)//            self.mainArray.removeObjectAtIndex(indexPath.row)//            self.maintableVIEw.reloadData()                        // 删除数据方法2(先删除数据,再删除cell)            self.mainArray.removeObjectAtIndex(indexPath.row)            self.maintableVIEw.deleteRowsAtIndexPaths([indexPath],withRowAnimation: UItableVIEwRowAnimation.None)        }        else if (editingStyle == UItableVIEwCellEditingStyle.Insert)        {            // 添加数据方法1(先添加数据,再重新加载全部数据)//            self.mainArray.addobject("添加数据")//            self.maintableVIEw.reloadData()                        // 添加数据方法2(先添加数据,再添加cell)            self.mainArray.addobject("添加数据")            self.maintableVIEw.insertRowsAtIndexPaths([NSIndexPath(forRow: (self.mainArray.count - 1),inSection: 0)],withRowAnimation: UItableVIEwRowAnimation.None)        }}
// MARK: 移动(注意:两个代理方法必须同时实现)    // cell可移动func tableVIEw(tableVIEw: UItableVIEw,canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {        return true}    // 移动cell事件func tableVIEw(tableVIEw: UItableVIEw,moveRowAtIndexPath fromIndexPath: NSIndexPath,toIndexPath: NSIndexPath) {                if fromIndexPath != toIndexPath        {            // 获取移动行对应的值            let itemValue = self.mainArray[fromIndexPath.row]            // 删除移动的值            self.mainArray.removeObjectAtIndex(fromIndexPath.row)                        // 如果移动区域大于现有行数,直接在最后添加移动的值            if toIndexPath.row > self.mainArray.count            {                self.mainArray.addobject(itemValue)            }            else            {                // 没有超过最大行数,则在目标位置添加刚才删除的值                self.mainArray.insertObject(itemValue,atIndex:toIndexPath.row)            }        }}
总结

以上是内存溢出为你收集整理的swift中UITableView的使用(编辑模式)全部内容,希望文章能够帮你解决swift中UITableView的使用(编辑模式)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1070696.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存