1.swift Create TableView Two classes Viewcontroller and Secondviewcontroller in this example
2. Create a navigation bar in Appdelegate, initialize with the view Code
1 func application (application:uiapplication, Didfinishlaunchingwithoptions launchoptions: [Nsobject:anyobject]?) -> Bool { // Override point for customization after application launch. 3 let Vc:viewcontroller = Viewcontroller () 4 let NAVIGATIONVC = Uinavigationcontroller (r OOTVIEWCONTROLLER:VC) 5 Self.window?. Rootviewcontroller = NAVIGATIONVC; 6 return true 7 }
In this explanation swift does not need to import the header file can be used directly, the code is more concise, such as the third line
The code in 3.viewController
classviewcontroller:uiviewcontroller,uitableviewdatasource,uitableviewdelegate{var page:int=1 //pagesvar custometableview:uitableview!var Thinglabel:uilabel?var Titlearr= ["first one","a second","a third"] Var dytabledatasource= [Anyobject] ()//Dynamic Datavar selectindexpath:nsindexpath!Overridefunc viewdidload () {super.viewdidload ()//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 {returnDatacount; } func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpath)-uitableviewcell{let Cellidentifier="Cellidentifier"Let cell=Tableview.dequeuereusablecellwithidentifier (Cellidentifier, Forindexpath:indexpath) Cell.textLabel?. Text =Titlearr[indexpath.row]returnCell} func tableView (Tableview:uitableview, Didselectrowatindexpath Indexpath:nsindexpath) { Let Secondvc:secondviewcontroller=Secondviewcontroller () Self.navigationcontroller?. Pushviewcontroller (SECONDVC, animated:true) } /** Set Title*/func confirtitle () {Self.title="my first Swift" }
The definition of variables in the code and their use, if necessary, can be referenced,
Swift code creation UITableView and part of Swift's explanation