0

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: "https://genietesting.herokuapp.com", protocolType: .HTTPS)
    var profile: [String]?
    let aboutGenie = [
        "How it works",
        "About",
        "Contact"
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

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

        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

airsoftFreak
  • 1,450
  • 5
  • 34
  • 64

4 Answers4

2

airsoftFreak,

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
        }
    }
Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
  • Thank you for the reply, I did everything you said, still getting nothing except the two headers. I updated my question with your suggestions. – airsoftFreak May 23 '16 at 09:06
  • @airsoftfreak : Dude atleast have a label and update its text with the content of aboutGenie array. There is no way that you wont get any cells it must be present just that you cant see it :) add a lable and set that label's text :) If you dont want to do so much coding simply set tableCell.textLabel.text = "Hi" you should be seeing two hi's in second section and one in first section :) – Sandeep Bhandari May 23 '16 at 09:44
  • @airsoftfreak : By the way an extra peice of info :) If your section header appears that means you have cells for that section :) If you declare a section header and set the number of rows to 0 then that section header will not appear :) As you have specified you can see both the section headers that means there are cells in both the sections :) Just that you can't see it :) Do what I said in my previous comment and you should see your cells :) – Sandeep Bhandari May 23 '16 at 09:48
  • Thank you it does show now if i set the label inside the tableview function, one last thing, how do i populate the data on the table cell? because I have two different data tuples – airsoftFreak May 23 '16 at 10:41
  • I have one very simple question, how do you modified the colour of the header? like making the box bigger – airsoftFreak May 23 '16 at 11:07
  • @airsoftfreak : In order to do that you cant use titleForHeaderInSection buddy you will have to use a delegate called viewForHeaderInSection :) Create a xib for a view with whatever the color you want and decorate it however you want then load the view from xib inside viewForHeaderInSection method and return it :) And as per height of the section header is concerned you can set the height you want for section header using another delegate named heightForHeaderInSection return whatever the height you want :D thats all buddy :) – Sandeep Bhandari May 23 '16 at 11:12
  • Seems pretty confusing, I will make another question for this. will update you in a sec – airsoftFreak May 23 '16 at 11:29
  • @airsoftfreak : Dont do that Ill give you a link go through you will understand the tableView delegates easily :) – Sandeep Bhandari May 23 '16 at 11:31
  • Im kinda confuse on where should I put the View on tableView, abit confused – airsoftFreak May 23 '16 at 11:32
  • and How would I link it to the table view – airsoftFreak May 23 '16 at 11:33
  • It is my personal observation that SO wont appreciate asking such basic doubts they will suggest you to search google and downVote :) Wait for sometime Ill provide you link with :) – Sandeep Bhandari May 23 '16 at 11:33
  • Have a look at it buddy :) http://www.ioscreator.com/tutorials/customizing-headers-footers-table-view-ios7 – Sandeep Bhandari May 23 '16 at 11:35
  • Have a looka t it for swift :) http://www.ioscreator.com/tutorials/customizing-header-footer-table-view-ios8-swift – Sandeep Bhandari May 23 '16 at 11:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/112676/discussion-between-sandeep-bhandari-and-airsoftfreak). – Sandeep Bhandari May 23 '16 at 11:37
0

Try to drag (ctrl+drag) the tableview to the yellow button at the top of the viewcontroller. You will now see to options: datasource and delegate. Choose one of these to and perform the action again for the other. Now the tableview should be linked to your code.

If the option to make it clickable was a question as well:

With the function didSelectRowAtIndexpath, you can achieve this. There should be a lot of stacks about this issue available.

Sven Cozijn
  • 119
  • 14
0

You probably have not wired the UITableView delegate and dataSource methods to the viewController. You can do this in two ways. 1. programatically create a tableViewOutlet

override fun viewDidLoad() {
   super.viewDidLoad()
   yourTableViewOutlet.delegate = self
   yourTableViewOutlet.dataSource = self
}
  1. in interfaceBuilder

    a) open the document outline in the storyboard. b) control drag from your tableView to your ViewController. c) connect delegate and dataSource one by one.

click on the cell will fire the delegate method didSelectRowAtIndexPath

Mathews
  • 733
  • 5
  • 11
  • I update my question with your suggestion but still no luck. – airsoftFreak May 23 '16 at 09:07
  • You haven't added any visual elements to your cell in the `cellForRowAtIndexPath` method. try doing it. Just `cell.textLabel.Text = yourArray[indexPath.row]` or `cell.backgroundColor = UIColor.greenColor` – Mathews May 23 '16 at 09:35
0

self.tableView.delegate and self.tableView.datasource

override func viewDidLoad() {
    super.viewDidLoad()
    let firstName = keychain[string: "first_name"]
    profile = [
        firstName!
    ]
    self.tableView.delegate = self
    self.tableView.datasource = self
}
Archie
  • 150
  • 6