zoukankan      html  css  js  c++  java
  • Swift

    1,下面的样例是给表格UITableView添加单元格移动功能: 

    (1)给表格添加长按功能,长按后表格进入编辑状态 
    (2)在编辑状态下,可以看到单元格后面出现拖动按钮 
    (3)鼠标按住拖动按钮,可以拖动单元格到任意位置
    (4)拖动完毕后,还会触发TabelView对应的代理事件

    2,效果图如下:
     

    3,代码如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    import UIKit
     
    class ViewController: UIViewController,UITableViewDelegate,
        UITableViewDataSource,UIGestureRecognizerDelegate {
         
        var tableView:UITableView?
         
        var ctrlnames:[String] = ["UILabel 标签","UIButton 按钮","UIDatePiker 日期选择器",
            "UITableView 表格视图"]
         
        override func viewDidLoad() {
            super.viewDidLoad()
             
            //创建表视图
            self.tableView = UITableView(frame: UIScreen.mainScreen().applicationFrame,
                style:UITableViewStyle.Plain)
            self.tableView!.delegate = self
            self.tableView!.dataSource = self
            //创建一个重用的单元格
            self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")
            self.view.addSubview(self.tableView!)
             
            //绑定对长按的响应
            var longPress =  UILongPressGestureRecognizer(target:self,
                action:Selector("tableviewCellLongPressed:"))
            //代理
            longPress.delegate = self
            longPress.minimumPressDuration = 1.0
            //将长按手势添加到需要实现长按操作的视图里
            self.tableView!.addGestureRecognizer(longPress)
        }
         
        //在本例中,只有一个分区
        func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
            return 1;
        }
         
        //返回表格行数(也就是返回控件数)
        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return self.ctrlnames.count
        }
         
        //创建各单元显示内容(创建参数indexPath指定的单元)
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
            -> UITableViewCell
        {
            //为了提供表格显示性能,已创建完成的单元需重复使用
            let identify:String = "SwiftCell"
            //同一形式的单元格重复使用,在声明时已注册
            let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath)
                as UITableViewCell
            cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
            cell.textLabel?.text = self.ctrlnames[indexPath.row]
            return cell
        }
         
        //长按表格
        func tableviewCellLongPressed(gestureRecognizer:UILongPressGestureRecognizer)
        {
            if (gestureRecognizer.state == UIGestureRecognizerState.Ended)
            {
                println("UIGestureRecognizerStateEnded");
                //在正常状态和编辑状态之间切换
                if(self.tableView!.editing == false){
                    self.tableView!.setEditing(true, animated:true)
                }
                else{
                    self.tableView!.setEditing(false, animated:true)
                }
            }
        }
         
        //在编辑状态,可以拖动设置cell位置
        func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
            return true
        }
         
        //移动cell事件
        func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath,
            toIndexPath: NSIndexPath) {
            if fromIndexPath != toIndexPath{
                //获取移动行对应的值
                var itemValue:String = ctrlnames[fromIndexPath.row]
                //删除移动的值
                ctrlnames.removeAtIndex(fromIndexPath.row)
                //如果移动区域大于现有行数,直接在最后添加移动的值
                if toIndexPath.row > ctrlnames.count{
                    ctrlnames.append(itemValue)
                }else{
                    //没有超过最大行数,则在目标位置添加刚才删除的值
                    ctrlnames.insert(itemValue, atIndex:toIndexPath.row)
                }
            }
        }
    }
  • 相关阅读:
    Shell脚本中cd命令使用
    OpenStack 的Nova组件详解
    Linux 查看网络连接状态
    Linux 怎么查看服务的启动进程所占用的目录
    邮政短信 运营商常见错误
    Linux 怎么把自己写的脚本添加到服务里面,即可以使用service命令来调用
    Linux 命令行生成随机密码的十种方法
    Linux rpm安装问题解决
    Codeforces 1082G(最大权闭合子图)
    Codeforces 1105D (BFS)
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/4843318.html
Copyright © 2011-2022 走看看