airsoftFreak airsoftFreak - 5 months ago 7x
Swift Question

Added a tableview on a viewController, but the data is not there

My goal is to make a grouped tableView, but for somehow the data is not added to the table View

Here's the story board picture

enter image description here

I added a table View on top of view controller which is
enter image description here

and the code that I wrote seems like it don't work

import UIKit
import Alamofire
import SwiftyJSON
import KeychainAccess

class SettingsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

let keychain = Keychain(server: "", protocolType: .HTTPS)
var profile: [String]?
let aboutGenie = [
"How it works",

override func viewDidLoad() {

let firstName = keychain[string: "first_name"]
profile = [

tableView.dataSource = self
tableView.delegate = self


func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return profile!.count
} else {
return aboutGenie.count

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "Profile"
} else {
return "About Genie"

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let tableCell = tableView.dequeueReusableCellWithIdentifier("myCell")
return tableCell!

and of course, I want to make it clickable so that it would go to its own viewController

After some suggestion, I changed most of my codes above and the result is still the same but this time it shows the header

The result is

enter image description here



There are multiple mistakes I can figure out

  1. There is no IBOutlet for your tableView which is added on top of your ViewController.

So you must be having something like

@IBOutlet var tableView: UITableView!
  1. Your SettingsViewController only confirms to UITableViewDataSource and not to UITableViewDelegate. If you wamt to get didSelectRowAtIndexPath to be triggerred you have to confirm to UITableViewDelegate

    class SettingsViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

  2. As many have noticed and mentioned in their answer you will have to set your viewController as delegate for both UITableViewDelegate,UITableViewDataSource so

    self.tableView.dataSource = self

    self.tableView.delegate = self

  3. The way you are instantiating cell is wrong as well :) Yopu should not create tableViewCell everytime for each cell :) Go to your TableView in storyBoard add a prototype cell, decorate it the way you want and the set the reusableIndentifier for it. Lets say reusableIndentifier you set is 'myCell'

your cellForRowAtIndexPath will change to

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //assuming you have different cells for each section
        switch indexPath.section {
        case 0: let tableCell = tableView.dequeueReusableCellWithIdentifier("myCell")
        tableCell.textLabel.text = profile[indexPath.row]
        return tableCell

            //in swift switch has to be exhaustive so default
        default: let secondSectionCell = tableView.dequeueReusableCellWithIdentifier("second_section_cell_identifier")
        secondSectionCell.textLabel.text =aboutGenie[indexPath.row]
        return secondSectionCell