Populate table view in swift

i0S Swift Issue

Question or problem in the Swift programming language:

Hi i want to populate this tableview

i don’t use the static table view because i take an error, and i decide to use the dynamic table view and disable the scroll in the table.
The table view is use only for the information :

  @IBOutlet var tableInfoQuake: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.title = quake.place


    tableInfoQuake.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.textLabel?.text = quake.place
    tableInfoQuake.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.textLabel?.text = "Mag: \(quake.mag)"
    tableInfoQuake.cellForRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.textLabel?.text = "Cordinate: (\(quake.longitude),\(quake.latitude))"
    tableInfoQuake.cellForRowAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.textLabel?.text = "Depth: \(quake.depth)"

but in the table view i don’t visualize nothing.
how can i do? thank you.

EDIT: i do this to modify each row :

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->   UITableViewCell {
    var cellIdentifier = "quakeInfo"
    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as UITableViewCell
    if indexPath == NSIndexPath(forRow: 0, inSection: 0){
        cell.textLabel!.text = quake.place
        cell.detailTextLabel!.text = "Mag: \(quake.mag)"
    }
    if indexPath == NSIndexPath(forRow: 1, inSection: 0){
      cell.textLabel!.text = "Cordinate: (\(quake.longitude),\(quake.latitude))"
        cell.detailTextLabel!.text = "Depth: \(quake.depth)"
    }

    return cell

}

How to solve the problem:

Solution 1:

Oh TableViews…

So, you want to dynamically populate a tableview. Well… here you go my friend:


Step 1: Implement The DataSource

Okay…. so you know what a protocol is? Well if you don’t … BAM, now you do (Not me in video). A DataSource is what the UITableView will call to retrieve the data it requires to display. To be specific, the DataSource in question is UITableViewDataSource.

so…

    // change top to...

    class MyViewController:UIViewController, UITableViewDataSource

This makes UITableViewDataSource a requirement for your ViewController. Next you need to set the datasource on your UITableView. So….

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = quake.place

        tableInfoQuake.datasource = self     
    }

Lets say you want to now add 7 cells to the table, and all 7 will say “Hello Man”. For this example, I am going to do it the worst yet simplest way. if you want me to go more in depth, just comment below, and I will do it the better way.

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->   UITableViewCell {
        let cell = UITableViewCell()
        let label = UILabel(CGRect(x:0, y:0, width:200, height:50))
        label.text = "Hello Man"
        cell.addSubview(label)
        return cell
    }

So thats it for step one. We have told the table that the ViewController has what it is looking for in terms of data, and we have returned data for it to use.


Step 2: Implement The Delegate

Much like Step 1, this step starts with adding something to the top…

    // change top to...

    class MyViewController:UIViewController, UITableViewDataSource, UITableViewDelegate

Then, again, much like the top, we will edit the viewDidLoad() function…

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = quake.place

        tableInfoQuake.datasource = self
        tableInfoQuake.delegate = self
    }

And now we must implement the delegate methods that designate the height of each cell.

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 50
    }

We have now added the delegate to the table, and implemented the method that tells the UITableView what the height of each cell is.


Lets Put It All Together

    class MyViewController:UIViewController, UITableViewDataSource, UITableViewDelegate {

        @IBOutlet var tableInfoQuake: UITableView!

        override func viewDidLoad() {
            super.viewDidLoad()

            tableInfoQuake.datasource = self
            tableInfoQuake.delegate = self
        }


        // UITableViewDataSource Functions

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

        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->   UITableViewCell {
            let cell = UITableViewCell()
            let label = UILabel(CGRect(x:0, y:0, width:200, height:50))
            label.text = "Hello Man"
            cell.addSubview(label)
            return cell
        }


        // UITableViewDelegate Functions

        func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
            return 50
        }        
    }

Good luck to you.

ZR

Solution 2:

The cells in the storyboard are prototype cells. That means they are not actually in the table view, but can be used as templates.

In order to populate the tableview, you need to:

  1. In the storyboard, set your view controller to be the datasource of the tableview by dragging the “datasource” connection from the connections menu of the table view to your view controller.
  2. Make your view controller conform to the UITableViewDataSource protocol.
  3. Implement the following methods in your view controller:
    • func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    • func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

Hope this helps!