UIViewController
UITableViewDelegate
UITableViewDataSource
{
// 表格加载
tableView:
UITableView
// 控件类型
ctrls = [
"UILabel"
"UIButton"
"UIImageView"
"UISlider"
]
//
detailViewController:
?
override
viewDidLoad() {
super
.viewDidLoad()
.title =
"Swift控件演示"
.tableView =
(frame:
.view.frame,style:
UITableViewStyle
.
Plain
)
.tableView!.delegate =
self
.tableView!.dataSource =
self
.tableView!.registerClass(
UITableViewCell
.
"SwiftCell"
)
.view.addSubview(
.tableView!)
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// UITableViewDataSource协议方法
tableView(tableView:
Int
) ->
Int
{
.ctrls.count
}
// UITableViewDataSource协议方法
NSIndexPath
)
->
UITableViewCell
{
cell = tableView.dequeueReusableCellWithIdentifier(
forIndexPath: indexPath)
cell.accessoryType =
UITableViewCellAccessoryType
DisclosureIndicator
cell.textLabel?.text =
.ctrls[indexPath.row]
return
cell
}
// UITableViewDelegate协议方法,点击时调用
)
{
//调用DetailViewController的方法更新详细页
detailViewController!.loadControl(
.ctrls[indexPath.row])
//如果是iPhone、iPod则导航到详情页
) {
// 跳转到detailViewController,取消选中状态
//self.tableView!.deselectRowAtIndexPath(indexPath,animated: true)
// navigationController跳转到detailViewController
.navigationController!.pushViewController(detailViewController!,animated:
true
)
}
}
--- DetailViewController.swift 详情页 ---
44
viewDidLoad() {
.viewDidLoad()
.view.backgroundColor =
.whiteColor()
ctrl =
.title !=
nil
?
.title! :
""
loadControl(ctrl)
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
loadControl(ctrl:
String
) {
clearViews()
switch
(ctrl) {
case
:
label =
UILabel
.view.bounds)
label.backgroundColor =
.clearColor()
label.textAlignment =
NSTextAlignment
Center
label.font =
UIFont
.systemFontOfSize(36)
label.text =
"Hello,Hangge.com"
.view.addSubview(label)
:
button =
UIButton
CGRectMake
(110,120,100,60))
button.backgroundColor =
.blueColor()
button.setTitleColor(
.redColor(),forState:
UIControlState
Normal
)
.whiteColor(),147)!important">Highlighted
)
button.setTitle(
"点击我"
)
.view.addSubview(button)
default
:
print
(
"clicked: (ctrl)"
}
clearViews() {
for
v
in
.view.subviews {
v.removeFromSuperview()
}
}
|
(注意:项目直接新建一个Master-Detail Application,就已经具有同上述一样的兼容iPhone、iPad的二级导航功能)
原文出自:
www.hangge.com
转载请保留原文链接:
http://www.hangge.com/blog/cache/detail_636.html (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|