1.swift创建TableView 本例子中 两个类 viewController 和SecondViewController
2.在AppDelegate创建导航条,初始化跟视图代码如图
1 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 2 // Override point for customization after application launch. 3 let vc:ViewController = ViewController() 4 let navigationVC = UINavigationController(rootViewController: vc) 5 self.window?.rootViewController = navigationVC; 6 return true 7 }
在此讲解swift不需要导入头文件了可以 直接用了,代码更加简练,如上图第三行
3.viewController中的代码如图
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{ var page : Int = 1 //页数 var customeTableView :UITableView! var thingLabel : UILabel? var titleArr = ["第一个","第二个","第三个"] var DYtableDataSource = [AnyObject]() //动态数据 var selectIndexPath : NSIndexPath! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.confirTitle() self.view.backgroundColor = UIColor.blueColor() customeTableView = UITableView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height), style: UITableViewStyle.Plain) customeTableView.delegate = self customeTableView.dataSource = self customeTableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cellIdentifier"); self.view.addSubview(customeTableView); } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1; } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return dataCount; } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ let cellIdentifier = "cellIdentifier" let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) cell.textLabel?.text = titleArr[indexPath.row] return cell } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let secondVC : SecondViewController = SecondViewController() self.navigationController?.pushViewController(secondVC, animated: true) } /** 设置标题 */ func confirTitle(){ self.title = "我的第一个swift" }
代码中的变量的定义以及使用如有需要可以参考,