功能如下:
效果图如下:
代码如下:
--- 入口文件AppDelegate.swift ---
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
import
UIKit
@UIApplicationMain
class
AppDelegate: UIResponder,UIApplicationDelegate {
var
window: UIWindow?
func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let rootViewController = ViewController()
let rootNavigationController = UINavigationController(rootViewController: rootViewController)
self.window!.rootViewController = rootNavigationController
return
true
}
func applicationWillResignActive(application: UIApplication) {
}
func applicationDidEnterBackground(application: UIApplication) {
}
func applicationWillEnterForeground(application: UIApplication) {
}
func applicationDidBecomeActive(application: UIApplication) {
}
func applicationWillTerminate(application: UIApplication) {
}
}
|
--- 主页面ViewController.swift ---
ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
// 表格加载
@H_930_301@tableView:UITableView?
// 控件类型
override
func viewDidLoad() {
super
.viewDidLoad()
self.title =
"Swift控件演示"
self.tableView = UITableView(frame:self.view.frame,style:UITableViewStyle.Plain)
self.tableView!.delegate = self
self.tableView!.dataSource = self
self.tableView!.registerClass(UITableViewCell.self,forCellReuseIdentifier:
"SwiftCell"
)
self.view.addSubview(self.tableView!)
func didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// UITableViewDataSource协议方法
func tableView(tableView: UITableView,numberOfRowsInSection section: Int) -> Int
{
return
self.ctrls.count
}
// UITableViewDataSource协议方法
-> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier(
as
UITableViewCell
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
cell.textLabel?.text = self.ctrls[indexPath.row]
cell
}
{
// 跳转到detailViewController,取消选中状态
self.tableView!.deselectRowAtIndexPath(indexPath,animated:
true
)
// 创建DetailViewController
let detailViewController = DetailViewController()
// 传递控件的title,在detailView里用于判断生成响应的控件
detailViewController.title = self.ctrls[indexPath.row]
// navigationController跳转到detailViewController
self.navigationController!.pushViewController(detailViewController,monospace!important; min-height:inherit!important">)
}
--- 子页面DetailViewController.swift ---
如果使用StoryBoard实现更加简单
AppDelegate.swift都不需要修改。打开Main.storyboard。
(1)点击首页的Scene,选择Editor -> Embed In -> Navigation Controller 即可。
(3)定义上面刚添加的Segue的Indentifier(比如detail)
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_586.html 原文链接:https://www.f2er.com/swift/324806.html 猜你在找的Swift相关文章 |