1,下面的样例是给表格UITableView添加单元格移动功能:
(1)给表格添加长按功能,长按后表格进入编辑状态
(2)在编辑状态下,可以看到单元格后面出现拖动按钮
(3)鼠标按住拖动按钮,可以拖动单元格到任意位置
(4)拖动完毕后,还会触发TabelView对应的代理事件
2,效果图如下:
![](http://www.hangge.com/blog_uploads/201506/2015060210084134433.png)
![](http://www.hangge.com/blog_uploads/201506/2015060210094148246.png)
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)
}
}
}
}
|