-
Notifications
You must be signed in to change notification settings - Fork 2
/
ViewController.swift
85 lines (54 loc) · 2.71 KB
/
ViewController.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// ViewController.swift
// iOS_Design_patterns_Demo
//
// Created by 曾明剑 on 15/8/19.
// Copyright © 2015年 zmj. All rights reserved.
//
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
@IBOutlet weak var tableView:UITableView!
var dataSource:Array<CellItem> = []
var nameList = ["工厂方法","单例","构建者","适配器","桥接","外观","命令","装饰器","职责链","策略"]
var viewControlles = ["FactoryViewController","SingletonViewController","BuilderViewController",
"AdapterViewController","BridgeViewController","FacadeViewController",
"CommandViewController","DecoratorViewController","ChainOfResViewController",
"StrategyViewController"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.title = "常用设计模式举例"
for index in 0 ..< nameList.count{
let item = CellItem()
item.text = nameList[index]
item.itemId = index
item.viewController = viewControlles[index]
self.dataSource.append(item)
}
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier:"cellId")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataSource.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cellId")!
let item:CellItem = self.dataSource[indexPath.row]
cell.textLabel!.text = item.text
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let item:CellItem = self.dataSource[indexPath.row]
if (item.viewController != nil){
if let vcclass = NSObject.SwiftClassFromString(item.viewController!) as? BaseViewController.Type{
let vc:BaseViewController = vcclass.init()
vc.naviTitle = item.text
self.navigationController?.pushViewController(vc, animated: true)
}
}
}
}