1,下面的样例是给表格UITableView添加编辑功能:

(1)给表格添加长按功能,长按后表格进入编辑状态
(2)在编辑状态下,第一个分组处于删除状态,第二个分组处于插入状态
(3)点击删除图标,删除对应条目
(4)点击添加图标,插入一条新数据
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import UIKit
 
class ViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource,UIGestureRecognizerDelegate{
     
    var tableView:UITableView?
     
    var allnames:Dictionary<Int, [String]>?
     
    var adHeaders:[String]?
     
    override func loadView() {
        super.loadView()
    }
     
    override func viewDidLoad() {
        super.viewDidLoad()
         
        //初始化数据,这一次数据,我们放在属性列表文件里
        self.allnames =  [
            0:[String]([
                "UILabel 标签",
                "UIButton 按钮"]),
            1:[String]([
                "UIDatePiker 日期选择器",
                "UITableView 表格视图"])
        ];
         
        println(self.allnames)
         
        self.adHeaders = [
            "常见 UIKit 控件",
            "高级 UIKit 控件"
        ]
         
        //创建表视图
        self.tableView = UITableView(frame:self.view.frame, style:UITableViewStyle.Grouped)
        self.tableView!.delegate = self
        self.tableView!.dataSource = self
        //创建一个重用的单元格
        self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")
        self.view.addSubview(self.tableView!)
         
        //创建表头标签
        var headerLabel = UILabel(frame: CGRectMake(0, 0, self.view.bounds.size.width, 30))
        headerLabel.backgroundColor = UIColor.blackColor()
        headerLabel.textColor = UIColor.whiteColor()
        headerLabel.numberOfLines = 0
        headerLabel.lineBreakMode = NSLineBreakMode.ByWordWrapping
        headerLabel.text = "UIKit 控件"
        headerLabel.font = UIFont.italicSystemFontOfSize(20)
        self.tableView!.tableHeaderView = headerLabel
         
        //绑定对长按的响应
        var longPress =  UILongPressGestureRecognizer(target:self,
            action:Selector("tableviewCellLongPressed:"))
        //代理
        longPress.delegate = self
        longPress.minimumPressDuration = 1.0
        //将长按手势添加到需要实现长按操作的视图里
        self.tableView!.addGestureRecognizer(longPress)
    }
      
    func tableviewCellLongPressed(gestureRecognizer:UILongPressGestureRecognizer)
    {
        if (gestureRecognizer.state == UIGestureRecognizerState.Began)
        {
            println("UIGestureRecognizerStateBegan");
        }
        if (gestureRecognizer.state == UIGestureRecognizerState.Changed)
        {
            println("UIGestureRecognizerStateChanged");
        }
         
        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)
                 
            }
        }
    }
     
    //在本例中,有2个分区
    func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
        return 2
    }
     
    //返回表格行数(也就是返回控件数)
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var data = self.allnames?[section]
        return data!.count
    }
     
    // UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的头部
    func tableView(tableView:UITableView, titleForHeaderInSection
        section:Int)->String
    {
        var headers =  self.adHeaders!;
        return headers[section];
    }
     
    // UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的尾部
    func tableView(tableView:UITableView, titleForFooterInSection
        section:Int)->String
    {
        var data = self.allnames?[section]
        return "有\(data!.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
         
        var secno = indexPath.section
        var data = self.allnames?[secno]
        cell.textLabel?.text = data![indexPath.row]
         
        return cell
    }
     
    // UITableViewDelegate 方法,处理列表项的选中事件
    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)
    {
        self.tableView!.deselectRowAtIndexPath(indexPath, animated: true)
         
        var itemString = self.allnames![indexPath.section]![indexPath.row]
         
        var alertview = UIAlertView();
        alertview.title = "提示!"
        alertview.message = "你选中了【\(itemString)】";
        alertview.addButtonWithTitle("确定")
        alertview.show();
    }
     
    func tableView(tableView: UITableView!, editingStyleForRowAtIndexPath indexPath: NSIndexPath!)
        -> UITableViewCellEditingStyle
    {
        if(indexPath.section == 1)
        {
            return UITableViewCellEditingStyle.Insert
        }
        return UITableViewCellEditingStyle.Delete
    }
     
    func tableView(tableView: UITableView!,
        titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath!) -> String!
    {
        var data = self.allnames?[indexPath.section]!
         
        var itemString = data![indexPath.row] as String
        return "确定删除\(itemString)?"
    }
     
    func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle,
        forRowAtIndexPath indexPath: NSIndexPath!)
    {
        if(editingStyle == UITableViewCellEditingStyle.Delete)
        {
            self.allnames?[indexPath.section]?.removeAtIndex(indexPath.row)
             
            self.tableView!.reloadData()
            self.tableView!.setEditing(true, animated: true)
            println("你确认了删除按钮")
           // Array
        }
        else if(editingStyle == UITableViewCellEditingStyle.Insert)
        {
            self.allnames?[indexPath.section]?.insert("插入一项新的", atIndex: indexPath.row+1)
            println("你按下了插入按钮")
            self.tableView!.reloadData()
        }
    }
     
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()       
        // Dispose of any resources that can be recreated.
    }
}

Swift - 给表格添加编辑功能(删除,插入)的更多相关文章

  1. 为HTML表格添加交互功能------DataTables

    DataTables是一个功能强大的Javascript库,用于为HTML表格添加交互功能,虽然简单性是整个项目的核心设计原则,但入门看起来相当艰巨.但是,采取这些第一步并在您的网站上运行DataTa ...

  2. swift - 表格的编辑功能(添加、删除)

    表格(tableview)的确是一个很好用的控件,现在来实现一个编辑功能的实现,包含添加和删除,删除包括长按删除和左滑删除 效果图如下: 具体代码如下: 1.创建表格(这个表格有2个区,有区头和区尾) ...

  3. Swift - 给表格添加移动单元格功能(拖动行)

    1,下面的样例是给表格UITableView添加单元格移动功能: (1)给表格添加长按功能,长按后表格进入编辑状态  (2)在编辑状态下,可以看到单元格后面出现拖动按钮  (3)鼠标按住拖动按钮,可以 ...

  4. el-table实现表格的编辑、删除、以及新增行的方法

    直接上代码: html部分: <el-form :model="inServForm" ref="inServForm" label-width=&quo ...

  5. Swift - 给表格添加Cell的显示动画(3D缩放)

    下面的一个样例是让tableView显示数据的时候具有一个很炫的3D缩放效果. 我们只需要实现tableView的willDisplayCell方法.看方法名就知道这是在Cell将要显示的时候执行的方 ...

  6. Linqpad使用(调试Linq、结合linq调试业务场景、表格内编辑数据)

      linqpad是一款linq语句调试工具,功能如下: 1.直接执行linq语句并查看生成的原生sql语句 2.可结合linq+C#代码进行业务场景调试 3.表格内直接新增.修改.删除数据 4.直接 ...

  7. C# 如何在Excel表格中插入、编辑和删除批注

    概述 为文档添加必要的批注可以给文档使用者提供重要的提示信息,下面的示例中,将介绍通过C#编程语言来给Excel表格中的指定单元格内容添加批注,此外,对于已有的批注,如果需要修改,我们也可以进行编辑或 ...

  8. C# 实现对PPT插入、编辑、删除表格

    现代学习和办公当中,经常会接触到对表格的运用,像各种单据.报表.账户等等.在PPT演示文稿中同样不可避免的应用到各种数据表格.对于在PPT中插入表格,我发现了一个新方法,不过我用到了一款免费的.NET ...

  9. JAVAEE——BOS物流项目09:业务受理需求分析、创建表、实现自动分单、数据表格编辑功能使用方法和工作单快速录入

    1 学习计划 1.业务受理需求分析 n 业务通知单 n 工单 n 工作单 2.创建业务受理环节的数据表 n 业务通知单 n 工单 n 工作单 3.实现业务受理自动分单 n 在CRM服务端扩展方法根据手 ...

随机推荐

  1. BZOJ 1854: [Scoi2010]游戏( 二分图最大匹配 )

    匈牙利算法..从1~10000依次找增广路, 找不到就停止, 输出答案. --------------------------------------------------------------- ...

  2. HDU4707:Pet(DFS)

    Problem Description One day, Lin Ji wake up in the morning and found that his pethamster escaped. He ...

  3. Android基础总结(精华完整版)

    1. 前言 1.1. 什么是3G.4G Ÿ 第三代移动通信技术(3rd - Generation),速率一般在几百Kbps,较之前的2G和2.5G在数据传输速度上有很大提升. Ÿ 第四代移动通信技术( ...

  4. SAP 金税接口介绍

    一.金税发票与SAP系统发票的税额差异分析 1.1 金税系统中的税额说明 国内企业销售产品给国内客户时,正常产品须要缴纳17%的增值税,而金税(Golden Tax)系统就是用来出具纸面的增值税发票的 ...

  5. nexus搭建

    Nexus是Maven仓库管理软件,可以用来搭建私服.主页 http://www.sonatype.org/nexus/ 下载地址 http://www.sonatype.org/nexus/down ...

  6. 1941. Scary Martian Word

    1941. Scary Martian Word 这道题 一个长度为3的字符串视为 一个 火星文 字母(ASCII 33-122) ,给出一个火星人认为恐怖的单词(由火星字母组成) 然后 给你一篇文章 ...

  7. AngularJS_百度百科

    AngularJS_百度百科     AngularJS    编辑     AngularJS是为克服HTML在构建应用上的不足而设计的.    目录         1简介引引        端对 ...

  8. Matlab,Visio等生成的图片的字体嵌入问题解决方法

    确保所有字体嵌入,是生成高质量学术论文的必要条件.但是在Windows下,总会遇到Matlab或Visio生成字体没有嵌入的问题,当然这个问题的解决办法有很多(例如,对于Visio可以这样做:直接拷贝 ...

  9. HBase概念学习(七)HBase与Mapreduce集成

    这篇文章是看了HBase权威指南之后,依据上面的解说搬下来的样例,可是略微有些不一样. HBase与mapreduce的集成无非就是mapreduce作业以HBase表作为输入,或者作为输出,也或者作 ...

  10. 网页内容的html标签补全和过滤的两种方法

    网页内容的html标签补全和过滤的两种方法: 假设你的网页内容的html标签显示不全,有些表格标签不完整而导致页面混乱,或者把你的内容之外的局部html页面给包括进去了,我们能够写个函数方法来补全ht ...