{"id":58,"date":"2015-11-10T20:01:12","date_gmt":"2015-11-10T20:01:12","guid":{"rendered":"http:\/\/luduscella.com\/blog\/\/?p=58"},"modified":"2015-11-11T18:38:40","modified_gmt":"2015-11-11T18:38:40","slug":"how-to-implement-uitableview-programmatically-using-swift","status":"publish","type":"post","link":"http:\/\/luduscella.com\/blog\/uitableview\/how-to-implement-uitableview-programmatically-using-swift","title":{"rendered":"How to implement UITableView programmatically using Swift"},"content":{"rendered":"<p>When implementing a UITableView in your iOS application you will usually select a master-detail application:<\/p>\n<p><img src=\"http:\/\/luduscella.com\/blog\/\/img\/master-detail-Swift.gif\" alt=\"Master Detail Application Swift\" \/><\/p>\n<p>Once the project is selected the UITableView implementation is ready for you to start adapting in your application, but sometimes your need just a tableView.<\/p>\n<p>The implementation of the tableView is very simple to do in Swift.  Start by implementing the following functions:<\/p>\n<pre class=\"lang:swift decode:true \" >\r\n\r\noptional public func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int \r\nfunc tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int\r\nfunc tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell\r\nfunc tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)\r\n\r\n<\/pre>\n<p>This function is optional per documentation: &#8220;Default is 1 if not implemented&#8221;<\/p>\n<pre class=\"lang:swift decode:true \" >\r\noptional public func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int \r\n<\/pre>\n<p>Lets start the implementation of our tableView.<\/p>\n<p>-Open Xcode and go to the file menu and select new and select new project or press Ctrl+Shift+N.<br \/>\n-Select the name of your project.<br \/>\n-Select the location where you want to save your project.<\/p>\n<p><img src=\"http:\/\/luduscella.com\/blog\/\/img\/single-viewApp-Swift.gif\" alt=\"Single View Application Swift\" \/><\/p>\n<p>Go to ViewController (ViewController.swift). In your ViewController you need to add the following delegates:<\/p>\n<p>UITableViewDelegate,UITableViewDataSource<\/p>\n<p>It will look like this:<\/p>\n<pre class=\"lang:swift decode:true \" >class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource<\/pre>\n<p>Now you need to add the variables. Choose a variable where you are going to store the objects you are going to display in your tableView:<\/p>\n<pre class=\"lang:swift decode:true \" >var tableView: UITableView  =   UITableView()\r\nlet animals : [String] = [\"Dogs\",\"Cats\",\"Mice\"]<\/pre>\n<p>Now you need to initialize and set your tableView variable:<\/p>\n<pre class=\"lang:swift decode:true \" >tableView = UITableView(frame: UIScreen.mainScreen().bounds, style: UITableViewStyle.Plain)\r\ntableView.delegate      =   self\r\ntableView.dataSource    =   self\r\ntableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: \"cell\")\r\nself.view.addSubview(self.tableView)<\/pre>\n<p>Because you are using only one section you are not going to implement the following function:<\/p>\n<pre class=\"lang:swift decode:true \" >optional public func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int <\/pre>\n<p>Now you need to get the count of elements you are going to display in your tableView:<\/p>\n<pre class=\"lang:swift decode:true \" >    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int\r\n    {\r\n        return animals.count\r\n        \r\n    }<\/pre>\n<p>Now you need to assign the values in your array variable to a cell:<\/p>\n<pre class=\"lang:swift decode:true \" >    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell\r\n    {\r\n        \r\n        let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: \"cell\")\r\n        cell.textLabel!.text = animals [indexPath.row]\r\n        return cell;\r\n    }<\/pre>\n<p>Now you need to register when user taps a cell:<\/p>\n<pre class=\"lang:swift decode:true \" >    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)\r\n    {\r\n        print(animals[indexPath.row])\r\n        \r\n    }<\/pre>\n<p>Your implementation should look like this:<\/p>\n<pre class=\"lang:swift decode:true \" >import UIKit\r\n\r\nclass ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {\r\n    var tableView: UITableView  =   UITableView()\r\n    let animals : [String] = [\"Dogs\",\"Cats\",\"Mice\"]\r\n    \r\n    override func viewDidLoad() {\r\n        super.viewDidLoad()\r\n        \r\n        tableView = UITableView(frame: UIScreen.mainScreen().bounds, style: UITableViewStyle.Plain)\r\n        tableView.delegate      =   self\r\n        tableView.dataSource    =   self\r\n        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: \"cell\")\r\n        self.view.addSubview(self.tableView)\r\n        \r\n        \r\n    }\r\n    \r\n    \r\n    \r\n    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int\r\n    {\r\n        return animals.count\r\n        \r\n    }\r\n    \r\n    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell\r\n    {\r\n        \r\n        let cell:UITableViewCell=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: \"cell\")\r\n        cell.textLabel!.text = animals [indexPath.row]\r\n        return cell;\r\n    }\r\n    \r\n    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)\r\n    {\r\n        print(animals[indexPath.row])\r\n        \r\n    }\r\n}<\/pre>\n<p>If you click on the Run in your Xcode project should look like this:<\/p>\n<p><img src=\"http:\/\/luduscella.com\/blog\/\/img\/uitableview-Swift.gif\" alt=\"UITableView Xcode Swift\" \/><\/p>\n<p>Please let me know if you have any questions by leaving a comment below or on twitter <a href=\"https:\/\/twitter.com\/luisemedr\" target=\"_blank\">@luisemedr<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When implementing a UITableView in your iOS application you will usually select a master-detail application: Once the project is selected the UITableView implementation is ready for you to start adapting in your application, but sometimes your need just a tableView. The implementation of the tableView is very simple to do in Swift. Start by implementing [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":[],"categories":[9,2],"tags":[],"_links":{"self":[{"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/posts\/58"}],"collection":[{"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/comments?post=58"}],"version-history":[{"count":11,"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/posts\/58\/revisions"}],"predecessor-version":[{"id":73,"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/posts\/58\/revisions\/73"}],"wp:attachment":[{"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/media?parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/categories?post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/luduscella.com\/blog\/wp-json\/wp\/v2\/tags?post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}