Swift - 给表格添加编辑功能删除,插入

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
import UIKit
class ViewController : UIViewController , UITableViewDelegate ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
UITableViewDataSource UIGestureRecognizerDelegate {
var tableView: UITableView ?
allnames: Dictionary < Int ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,[ String ]>?
adHeaders:[ ]?
override func loadView() {
super .loadView()
}
viewDidLoad() {
.viewDidLoad()
//初始化数据,这一次数据,我们放在属性列表文件
self .allnames = [
0:[ ]([
"UILabel 标签" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"UIButton 按钮" ]),
1:[ String ]([
"UIDatePiker 日期选择器" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"UITableView 表格视图" ])
];
print ( .allnames)
.adHeaders = [
"常见 UIKit 控件" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"高级 UIKit 控件"
]
//创建表视图
.tableView = (frame: .view.frame,style: UITableViewStyle . Grouped )
.tableView!.delegate = self
.tableView!.dataSource = self
//创建一个重用的单元格
.tableView!.registerClass( UITableViewCell . ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,forCellReuseIdentifier: "SwiftCell" )
.view.addSubview( .tableView!)
//创建表头标签
let headerLabel = UILabel (frame: CGRectMake (0, .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)
.tableView!.tableHeaderView = headerLabel
//绑定对长按的响应
longPress = UILongPressGestureRecognizer (target: ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
action: Selector ( "tableviewCellLongpressed:" ))
//代理
longPress.delegate = self
longPress.minimumPressDuration = 1.0
//将长按手势添加到需要实现长按操作的视图里
.tableView!.addGestureRecognizer(longPress)
}
tableviewCellLongpressed(gestureRecognizer: )
{
if (gestureRecognizer.state == UIGestureRecognizerState Began )
{
"UIGestureRecognizerStateBegan" );
}
Changed )
{
"UIGestureRecognizerStateChanged" );
}
(gestureRecognizer.state == UIGestureRecognizerState Ended )
{
"UIGestureRecognizerStateEnded" );
//在正常状态和编辑状态之间切换
.tableView!.editing == false )
{
.tableView!.setEditing( true ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,animated: )
}
else
{
)
}
}
}
//在本例中,有2个分区
numberOfSectionsInTableView(tableView: UITableView ) -> {
return 2
}
//返回表格行数(也就是返回控件数)
tableView(tableView: ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,numberOfRowsInSection section: {
data = .allnames?[section]
data!.count
}
// UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的头部
tableView(tableView: ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,titleForHeaderInSection
section: )-> ?
{
headers = .adHeaders!;
headers[section];
}
// UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的尾部
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:none rgb(249,titleForFooterInSection
?
{
.allnames?[section]
return "有\(data!.count)个控件"
}
//创建各单元显示内容(创建参数indexPath指定的单元)
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:none rgb(249,cellForRowAtIndexPath indexPath: NSIndexPath )
-> UITableViewCell
{
//为了提供表格显示性能,已创建完成的单元需重复使用
identify: = "SwiftCell"
//同一形式的单元格重复使用,在声明时已注册
cell = tableView.dequeueReusableCellWithIdentifier(identify,forIndexPath: indexPath)
as UITableViewCell
cell.accessoryType = UITableViewCellAccessoryType disclosureIndicator
secno = indexPath.section
.allnames?[secno]
cell.textLabel?.text = data![indexPath.row]
cell
}
// UITableViewDelegate 方法,处理列表项的选中事件
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:none rgb(249,didSelectRowAtIndexPath indexPath: )
{
.tableView!.deselectRowAtIndexPath(indexPath,monospace!important; min-height:inherit!important; background:none!important">)
itemString = .allnames![indexPath.section]![indexPath.row]
alertview = UIAlertView ();
alertview.title = "提示!"
alertview.message = "你选中了【\(itemString)】" ;
alertview.addButtonWithTitle( "确定" )
alertview.show();
}
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:none rgb(249,editingStyleForRowAtIndexPath indexPath: NSIndexPath )
-> UITableViewCellEditingStyle
{
(indexPath.section == 1)
{
return UITableViewCellEditingStyle Insert
}
return UITableViewCellEditingStyle Delete
}
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:none rgb(249,
titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: ) -> ?
{
.allnames?[indexPath.section]!
itemString = data![indexPath.row] as String
"确定删除\(itemString)?"
}
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important; background:none rgb(249,commitEditingStyle editingStyle: ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
forRowAtIndexPath indexPath: )
{
(editingStyle == Delete )
{
.allnames?[indexPath.section]?.removeAtIndex(indexPath.row)
.tableView!.reloadData()
)
"你确认了删除按钮" )
// Array
}
else (editingStyle == Insert )
{
.allnames?[indexPath.section]?.insert( "插入一项新的" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,atIndex: indexPath.row+1)
"你按下了插入按钮" )
.tableView!.reloadData()
}
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...