import
UIKit
class
ViewController
:
UIViewController
,
UITableViewDelegate
,
UITableViewDataSource
{
var
tableView:
UITableView
?
var
allnames:
Dictionary
<
Int
, [
String
]>?
var
adHeaders:[
String
]?
override
func
loadView() {
super
.loadView()
}
override
func
viewDidLoad() {
super
.viewDidLoad()
self
.allnames = [
0:[
String
]([
"UILabel 标签"
,
"UITextField 文本框"
,
"UIButton 按钮"
]),
1:[
String
]([
"UIDatePiker 日期选择器"
,
"UIToolbar 工具条"
,
"UITableView 表格视图"
])
];
println
(
self
.allnames)
self
.adHeaders = [
"常见 UIKit 控件"
,
"高级 UIKit 控件"
]
self
.tableView =
UITableView
(frame:
self
.view.frame, style:
UITableViewStyle
.
Grouped
)
self
.tableView!.delegate =
self
self
.tableView!.dataSource =
self
self
.tableView!.registerClass(
UITableViewCell
.
self
, forCellReuseIdentifier:
"SwiftCell"
)
self
.view.addSubview(
self
.tableView!)
var
headerLabel =
UILabel
(frame:
CGRectMake
(0, 0,
self
.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)
self
.tableView!.tableHeaderView = headerLabel
}
func
numberOfSectionsInTableView(tableView:
UITableView
!) ->
Int
{
return
2;
}
func
tableView(tableView:
UITableView
, numberOfRowsInSection section:
Int
) ->
Int
{
var
data =
self
.allnames?[section]
return
data!.count
}
func
tableView(tableView:
UITableView
, titleForHeaderInSection
section:
Int
)->
String
{
var
headers =
self
.adHeaders!;
return
headers[section];
}
func
tableView(tableView:
UITableView
, titleForFooterInSection
section:
Int
)->
String
{
var
data =
self
.allnames?[section]
return
"有\(data!.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
var
secno = indexPath.section
var
data =
self
.allnames?[secno]
cell.textLabel?.text = data![indexPath.row]
return
cell
}
func
tableView(tableView:
UITableView
!, didSelectRowAtIndexPath indexPath:
NSIndexPath
!)
{
self
.tableView!.deselectRowAtIndexPath(indexPath, animated:
true
)
var
itemString =
self
.allnames![indexPath.section]![indexPath.row]
var
alertview =
UIAlertView
();
alertview.title =
"提示!"
alertview.message =
"你选中了【\(itemString)】"
;
alertview.addButtonWithTitle(
"确定"
)
alertview.show();
}
override
func
didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
}
请发表评论