Friday, 31 October 2014

  1. How to make a simple Custom  tableView with iOS 8 and Swift


The first thing came to mind when we think for  Swift was TableViews. We will see step by step tutorial .

Open Xcode and Create a new project

Open Xcode 6+ version, create a new “Single Page Application” and select Swift as the programming language.


Add a table view property

Open the demoViewController.swift class and add a new tableview instance variable below the class declaration.

    @IBOutlet
    var tableView: UITableView

The @IBOutlet attribute makes the tableView property visible in Interface Builder.

Adopt the TableView Delegate and DataSource protocols by Confirming that 

Simply to conform to the UITableViewDelegate and UITableViewDataSource protocol simply adopt them seperated by colons afterSuperClass i.e. UIViewController . Might be it is bit confusing as the syntax has changed for protocol .

class demoViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    ...

}
After  doing that , as we adopted that protocols now we need to implements delegates and datasource as used  to do in Objective -C  tableView(_:numberOfRowsInSection:)tableView(_:cellForRowAtIndexPath:) and
tableView(_:didSelectRowAtIndexPath:) methods in the ViewController class .
class demoViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    ...
// delegates and data source methods
    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {    
        return 0 // number of rows 
    }

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        return nil
    }

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

    }

}

Simply add the table view in View Controller

Drag the table view from the objects in the View controller 
Dragging the table view

Make the connection with the Interface Builder Outlets

Simple by right clicking on the view controller add the respective controller and make sure that 
Connecting the interface outlets


Registering the cell with Class

 Inside ViewDidLoad method call the registerClass(_:forCellReuseIdentifier:)
class demoViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    ...

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

    ...

}

You can add some data to display the data inside table view 


Simply you can add the property data as an array of Strings and set some values :
class demoViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var data: String[] = ["One", "Two", "Three"]

    ...

}

After that set the number of rows for table view 


class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    ...

    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return self.items.count;
    }

    ...

}


Create the cell for custom class 


class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    ...

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

        cell.textLabel.text = self.data[indexPath.row]

        return cell
    }

    ...

}

Customize the didSelect Delegate Method 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    ...

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        println("You selected cell #\(indexPath.row)!")
    }

    ...

}
After doing all that stuff now your code look like this : 
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet
    var tableView: UITableView
    var items: String[] = ["One", "Two", "Three"]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }


    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return self.items.count;
    }

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

        cell.textLabel.text = self.items[indexPath.row]

        return cell
    }

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        println("You selected cell #\(indexPath.row)!")
    }
}

This should be the expected output  after running the app:
Custom Table View Controller


You can the download source code : here
For any query you can leave the comments and point out any mistakes .

If it seems useful please share with friends .
Related Links:
Sqlite