import
UIKit
class
ViewController
:
UIViewController
,
UITableViewDelegate
UITableViewDataSource
{
var
ctrlnames:[
String
]?
tableView:
UITableView
?
override
func
loadView() {
super
.loadView()
}
viewDidLoad() {
.viewDidLoad()
self
.ctrlnames =
NSArray
(contentsOfFile:
NSBundle
.mainBundle().pathForResource(
"Controls"
"plist"
)!)
as
?
Array
print
(
.ctrlnames)
//创建表视图
.tableView =
UITableView
(frame:
.view.frame,style:
UITableViewStyle
.
Plain
)
.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 =
.whiteColor()
headerLabel.numberOfLines = 0
headerLabel.lineBreakMode =
NSLineBreakMode
ByWordWrapping
headerLabel.text =
"常见 UIKit 控件"
headerLabel.font =
UIFont
.italicSystemFontOfSize(20)
.tableView!.tableHeaderView = headerLabel
}
//在本例中,只有一个分区
numberOfSectionsInTableView(tableView:
) ->
Int
{
return
1;
}
//返回表格行数(也就是返回控件数)
tableView(tableView:
{
return
.ctrlnames!.count
}
//创建各单元显示内容(创建参数indexPath指定的单元)
tableView(tableView:
NSIndexPath
)
->
UITableViewCell
{
//为了提供表格显示性能,已创建完成的单元需重复使用
identify:
=
"SwiftCell"
//同一形式的单元格重复使用,在声明时已注册
cell = tableView.dequeueReusableCellWithIdentifier(identify,forIndexPath: indexPath)
as
UITableViewCell
cell.accessoryType =
UITableViewCellAccessoryType
DisclosureIndicator
cell.textLabel?.text =
.ctrlnames![indexPath.row]
cell
}
// UITableViewDelegate 方法,处理列表项的选中事件
)
{
.tableView!.deselectRowAtIndexPath(indexPath,animated:
true
)
itemString =
.ctrlnames![indexPath.row]
alertController =
UIAlertController
(title:
"提示!"
message:
"你选中了【(itemString)】"
UIAlertControllerStyle
Alert
)
okAction =
UIAlertAction
"确定"
UIAlertActionStyle
Default
nil
)
alertController.addAction(okAction)
.presentViewController(alertController,completion:
)
}
//滑动删除必须实现的方法
commitEditingStyle editingStyle:
UITableViewCellEditingStyle
forRowAtIndexPath indexPath:
) {
(
"删除(indexPath.row)"
)
index = indexPath.row
.ctrlnames?.removeAtIndex(index)
.tableView?.deleteRowsAtIndexPaths([indexPath],
withRowAnimation:
UITableViewRowAnimation
Top
)
}
//滑动删除
NSIndexPath
)
{
return
UITableViewCellEditingStyle
Delete
}
//修改删除按钮的文字
titleForDeleteConfirmationButtonForRowAtIndexPath indexPath:
String
? {
return
"删"
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}