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: - click
func 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,UITableViewDelegate
func 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的使用编辑模式的更多相关文章

  1. HTML5 input新增type属性color颜色拾取器的实例代码

    type 属性规定 input 元素的类型。本文较详细的给大家介绍了HTML5 input新增type属性color颜色拾取器的实例代码,感兴趣的朋友跟随脚本之家小编一起看看吧

  2. amazeui模态框弹出后立马消失并刷新页面

    这篇文章主要介绍了amazeui模态框弹出后立马消失并刷新页面,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  3. 移动HTML5前端框架—MUI的使用

    这篇文章主要介绍了移动HTML5前端框架—MUI的使用的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  4. HTML5 weui使用笔记

    这篇文章主要介绍了HTML5 weui使用笔记,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  5. Html5写一个简单的俄罗斯方块小游戏

    这篇文章主要介绍了基于Html5写一个简单的俄罗斯方块小游戏,本文通过图文并茂的形式给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友参考下吧

  6. AmazeUI 模态窗口的实现代码

    这篇文章主要介绍了AmazeUI 模态窗口的实现代码,代码简单易懂,非常不错,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  7. ios – UITableView和Cell Reuse

    这是我的CustomCell类的init方法解决方法如果没有要显示的图像,则必须清除图像视图:

  8. ios – fetchedResultsController.fetchedObjects.count = 0但它充满了对象

    我正在使用相当标准的fetchedResultsController实现来输出tableView.在-viewDidLoad的最后,我正在进行第一次调用:这是我的fetchedResultsController:我的tableView方法:所以,问题是:在_fetchedResultsController.fetchedobjects.count的日志中等于0,但在视觉上tableView充满了对

  9. ios – UIPopoverController出现在错误的位置

    所以我花了一些时间寻找答案,但到目前为止还没有找到任何答案.我正在尝试从UIInputAccessoryView上的按钮呈现弹出窗口.UIBarButtonItem我想显示popover来自定制视图,所以我可以使用图像.我创建这样的按钮:当需要显示popover时,我这样做:但我得到的是:弹出窗口看起来很好,但它应该出现在第一个按钮上时出现在第二个按钮上.然后我发现了这个问题:UIBarButto

  10. ios – UITableView在滚动时阻止重新加载

    或者你能想象一个防止这种行为的好方法吗?解决方法抱歉,我没有足够的声誉来添加评论,因此在单独的答案中回答您的上一个问题.-performSelector:withObject:afterDelay:延迟为0.0秒不会立即执行给定的选择器,而是在当前的RunloopCycle结束后和给定的延迟之后执行它.-performSelector:withObject:添加到当前Runloop循环中并执行.这与直接调用该方法相同.因此,使用-performSelector:withObject:afterDelay:

随机推荐

  1. Swift UITextField,UITextView,UISegmentedControl,UISwitch

    下面我们通过一个demo来简单的实现下这些控件的功能.首先,我们拖将这几个控件拖到storyboard,并关联上相应的属性和动作.如图:关联上属性和动作后,看看实现的代码:

  2. swift UISlider,UIStepper

    我们用两个label来显示slider和stepper的值.再用张图片来显示改变stepper值的效果.首先,这三个控件需要全局变量声明如下然后,我们对所有的控件做个简单的布局:最后,当slider的值改变时,我们用一个label来显示值的变化,同样,用另一个label来显示stepper值的变化,并改变图片的大小:实现效果如下:

  3. preferredFontForTextStyle字体设置之更改

    即:

  4. Swift没有异常处理,遇到功能性错误怎么办?

    本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请发送邮件至dio@foxmail.com举报,一经查实,本站将立刻删除。

  5. 字典实战和UIKit初探

    ios中数组和字典的应用Applicationschedule类别子项类别名称优先级数据包contactsentertainment接触UIKit学习用Swift调用CocoaTouchimportUIKitletcolors=[]varbackView=UIView(frame:CGRectMake(0.0,0.0,320.0,CGFloat(colors.count*50)))backView

  6. swift语言IOS8开发战记21 Core Data2

    上一话中我们简单地介绍了一些coredata的基本知识,这一话我们通过编程来实现coredata的使用。还记得我们在coredata中定义的那个Model么,上面这段代码会加载这个Model。定义完方法之后,我们对coredata的准备都已经完成了。最后强调一点,coredata并不是数据库,它只是一个框架,协助我们进行数据库操作,它并不关心我们把数据存到哪里。

  7. swift语言IOS8开发战记22 Core Data3

    上一话我们定义了与coredata有关的变量和方法,做足了准备工作,这一话我们来试试能不能成功。首先打开上一话中生成的Info类,在其中引用头文件的地方添加一个@objc,不然后面会报错,我也不知道为什么。

  8. swift实战小程序1天气预报

    在有一定swift基础的情况下,让我们来做一些小程序练练手,今天来试试做一个简单地天气预报。然后在btnpressed方法中依旧增加loadWeather方法.在loadWeather方法中加上信息的显示语句:运行一下看看效果,如图:虽然显示出来了,但是我们的text是可编辑状态的,在storyboard中勾选Editable,再次运行:大功告成,而且现在每次单击按钮,就会重新请求天气情况,大家也来试试吧。

  9. 【iOS学习01】swift ? and !  的学习

    如果不初始化就会报错。

  10. swift语言IOS8开发战记23 Core Data4

    接着我们需要把我们的Rest类变成一个被coredata管理的类,点开Rest类,作如下修改:关键字@NSManaged的作用是与实体中对应的属性通信,BinaryData对应的类型是NSData,CoreData没有布尔属性,只能用0和1来区分。进行如下操作,输入类名:建立好之后因为我们之前写的代码有些地方并不适用于coredata,所以编译器会报错,现在来一一解决。

返回
顶部