Swift code creation UITableView and part of Swift's explanation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.