4

I'm designing an app for my school and I am trying to make a simple directory using a tableview within a view controller. I began by trying to make objects, but then switched to a more simple array, still without any luck. I do not know if it is a problem with the code or with the storyboard. Here is the code for the viewcontroller:

import UIKit

class DirectoryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var backButton2: UIButton!

    @IBOutlet weak var directoryTableView: UITableView!

    // @IBOutlet weak var nameLabel: NSLayoutConstraint!

    // let searchController = UISearchController(searchResultsController: nil)

    // var directoryObjects: NSMutableArray! = NSMutableArray()

    var names = ["Teacher 1", "Teacher 2", "Teacher 3"]

    override func viewDidLoad() {
        super.viewDidLoad()

        /*  
        self.directoryObjects.addObject("Teacher 1")
        self.directoryObjects.addObject("Teacher 2")
        self.directoryObjects.addObject("Teacher 3")

        self.directoryTableView.reloadData()
        */
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    // Mark - tableview

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let directoryCell = self.directoryTableView.dequeueReusableCellWithIdentifier("directoryCell", forIndexPath: indexPath) as! DirectoryTableViewCell

        // directoryCell.directoryLabel.text = self.directoryObjects.objectAtIndex(indexPath.row) as? String

        directoryCell.directoryLabel.text = names[indexPath.row]

        return directoryCell
    }


    @IBAction func backButton2Tapped(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }

}

Here is the code for the DirectoryTableViewCell:

import UIKit

class DirectoryTableViewCell: UITableViewCell {

    @IBOutlet weak var directoryLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
/*
    func directoryTableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    }
    */
}
danywarner
  • 928
  • 2
  • 15
  • 28
themc1r
  • 49
  • 3
  • 3
    Set the delegates. – Wyetro Jul 27 '16 at 15:47
  • would that be in the code or in the storyboard? – themc1r Jul 27 '16 at 15:54
  • Either, it's up to you. I'll add instructions as an answer – Wyetro Jul 27 '16 at 15:55
  • `self.directoryTableView.delegate = self` and `self.directoryTableView.datasource = self` in` viewDidLoad` – Yogesh Suthar Jul 27 '16 at 15:58
  • 1
    Or in the storyboard, you can drag from the `tableView` to your `DirectoryViewController` (either the yellow circle with the white square at the top of the view, or in the document outline) and select both `dataSource` and `delegate`. Or even once you have selected the `tableView` drag from the `dataSource` and `delegate` options under the outlets heading in the connections inspector on the right of the page (utilities panel) to the view controller. – Matt Le Fleur Jul 27 '16 at 16:03
  • 1
    It has been answered already here. http://stackoverflow.com/a/25545185/3947151 – Tesan3089 Jul 27 '16 at 16:58
  • Possible duplicate of [Custom UITableViewCell from nib in Swift](http://stackoverflow.com/questions/25541786/custom-uitableviewcell-from-nib-in-swift) – Wyetro Jul 27 '16 at 18:14

1 Answers1

0

You have to set the delegates. The easiest way is in the code.

In viewDidLoad add:

directoryTableView.delegate = self
directoryTableView.dataSource = self

This will allow those tableView functions to get called.

I'd also recommend changing:

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

to:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return names.count
}
Wyetro
  • 8,439
  • 9
  • 46
  • 64
  • now it is showing the error EXC_BAD_INSTRUCTION to the line directoryCell.directoryLabel.text = names[indexPath.row] – themc1r Jul 27 '16 at 16:04
  • Well I don't know anything about you `DirectoryTableViewCell`, but it should be working otherwise. – Wyetro Jul 27 '16 at 16:09
  • 1
    Hmm, in your `cellForRowAtIndexPath` function, when defining your cell, try changing `self.directoryTableView` to `tableView` in the first line. – Matt Le Fleur Jul 27 '16 at 16:10
  • 1
    Also have you created a prototype cell with the identifier specified? – Wyetro Jul 27 '16 at 16:11
  • Yes in storyboard I created a prototype cell. It has a label that is linked to the DirectoryTableViewCell and the cell identifier is directoryCell. – themc1r Jul 27 '16 at 16:23
  • Try commenting out that line and see if it runs – Wyetro Jul 27 '16 at 16:24
  • @WMios I commented out that line and now it runs and there are three cells, but they all just say "Label" which is what the prototype cell says. – themc1r Jul 27 '16 at 16:28
  • So then it seems that you have an issue with how the label is setup. Do you need the custom class? Why not just set the titleLabel which is built into the UITableViewCell class? – Wyetro Jul 27 '16 at 16:50
  • I would say your initial question has been answered. I would look into other people who have had this next issue (http://stackoverflow.com/questions/4967554/problem-with-custom-label-in-uitableviewcell) – Wyetro Jul 27 '16 at 16:52