How to make a simple tableview with iOS 8 and Swift

Create a new project

Start Xcode 6, make a new “Single Web page Application” and choose Instant as the development terminology. 

Add a table view property

Start the ViewController.swift category and add a new tableview example varying below the category announcement.

    @IBOutlet
    var tableView: UITableView! 
 
The @IBOutlet attribute makes the tableView property visible in Interface Builder.

Conform to the TableView Delegate and DataSource protocols

To comply with the UITableViewDelegate and UITableViewDataSource method just add them seperated by colons afterUIViewController in the category announcement. This was a bit complicated at first but the new method format is better.

 but the new protocol syntax is cleaner.
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    ...

}
After that we need to implement the tableView(_:numberOfRowsInSection:)tableView(_:cellForRowAtIndexPath:) and
tableView(_:didSelectRowAtIndexPath:) methods in the ViewController class and leave them empty for now

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    ...

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }

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

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

    }

}

Add a table view in your view controller

Look for in the componet library(It should be in the reduced right corener) for Desk Perspective and then move and fall it on the scene.

Dragging the table view

Connect the Interface Builder Outlets

Link the referencing, dataSource and assign sites.

We can connect the specific sites using interface designer by right clicking on the table perspective and pulling each outlet on the perspective operator.
 Connecting the interface outlets
 Register the cell class

In the viewDidLoad technique contact the registerClass(_:forCellReuseIdentifier:). Tip: to get the category use the category name followed by .self
 
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    ...

    override func viewDidLoad() {
        super.viewDidLoad()

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

    ...

}
  
Add some data to display

Add a property called items as an Array of Strings and set some values
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var items: [String] = ["We", "Heart", "Swift"]

    ...

}

Set the number of rows

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    ...

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

    ...

}

Create the cell

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.items[indexPath.row]

        return cell
    }

    ...

}

Handle cell selection

Another new thing in Swift is String Interpolation that let’s you add arbitrary code inside a string.
Some examples:
var one = "\(1)" // "1"
var two = "\(1 + 1)" // "2"

var name = "Andrei Puni"
println("my name is \(name) and I heart Swift") // will print "my name is Andrei Puni and I heart Swift"
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    ...

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

    ...

}
By now your ViewController class should look like this:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet
    var tableView: UITableView
    var items: [String] = ["We", "Heart", "Swift"]

    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)!")
    }
}
If you haven’t skipped any steps you should be able to run the app and see something similar to:

A simple table view using iOS 8 and Swift Programming language

 In case you missed anything you could try our source code.

1 comments :

Write comments
veera
AUTHOR
30 August 2020 at 23:07 delete

Very nice post,thank you for sharing this useful article with us.

Thank you..

ios online training

Reply
avatar