import
UIKit
class
ViewController
:
UIViewController
,
UITableViewDelegate
UITableViewDataSource
UIGestureRecognizerDelegate
{
var
tableView:
UITableView
?
allnames:
Dictionary
<
Int
String
]>?
adHeaders:[
]?
override
func
loadView() {
super
.loadView()
}
viewDidLoad() {
.viewDidLoad()
self
.allnames = [
0:[
]([
"UILabel 标签"
"UIButton 按钮"
]),
1:[
String
]([
"UIDatePiker 日期选择器"
"UITableView 表格视图"
])
];
print
(
.allnames)
.adHeaders = [
"常见 UIKit 控件"
"高级 UIKit 控件"
]
//创建表视图
.tableView =
(frame:
.view.frame,style:
UITableViewStyle
.
Grouped
)
.tableView!.delegate =
self
.tableView!.dataSource =
self
//创建一个重用的单元格
.tableView!.registerClass(
UITableViewCell
.
"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:
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
)
}
else
{
)
}
}
}
//在本例中,有2个分区
numberOfSectionsInTableView(tableView:
UITableView
) ->
{
return
2
}
//返回表格行数(也就是返回控件数)
tableView(tableView:
{
data =
.allnames?[section]
data!.count
}
// UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的头部
tableView(tableView:
section:
)->
?
{
headers =
.adHeaders!;
headers[section];
}
// UITableViewDataSource协议中的方法,该方法的返回值决定指定分区的尾部
?
{
.allnames?[section]
return
"有(data!.count)个控件"
}
//创建各单元显示内容(创建参数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 方法,处理列表项的选中事件
)
{
.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();
}
NSIndexPath
)
->
UITableViewCellEditingStyle
{
(indexPath.section == 1)
{
return
UITableViewCellEditingStyle
Insert
}
return
UITableViewCellEditingStyle
Delete
}
titleForDeleteConfirmationButtonForRowAtIndexPath indexPath:
) ->
?
{
.allnames?[indexPath.section]!
itemString = data![indexPath.row]
as
String
"确定删除(itemString)?"
}
forRowAtIndexPath indexPath:
)
{
(editingStyle ==
Delete
)
{
.allnames?[indexPath.section]?.removeAtIndex(indexPath.row)
.tableView!.reloadData()
)
"你确认了删除按钮"
)
// Array
}
else
(editingStyle ==
Insert
)
{
.allnames?[indexPath.section]?.insert(
"插入一项新的"
"你按下了插入按钮"
)
.tableView!.reloadData()
}
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}