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
}
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
)
}
}
}
func
tableView(tableView:
UITableView
, canMoveRowAtIndexPath indexPath:
NSIndexPath
) ->
Bool
{
return
true
}
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)
}
}
}
}
请发表评论