0

This is my history view, I have a tableview inside view controller. But for some reason i can't output anything, I am trying to hardcode it and its not displaying anything at all. One thing i know is i need to make tableView functions override and if i do that i get error.

  import UIKit

    class History: UIViewController, UITableViewDataSource, UITableViewDelegate
    {

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self

        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "newBackground.jpg")!)
    }

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


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

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("historyCell", forIndexPath: indexPath) as! historyCell

        cell.durationLabel.text = "98"

        return cell
    }


    override func viewWillAppear(animated: Bool)
    {
        super.viewWillAppear(animated)

    }

}

heres my class for the cell prototype, and i made sure that the identifier is also "historyCell"

public class historyCell: UITableViewCell{
    @IBOutlet var durationLabel: UILabel!
    @IBOutlet var kicksLabel: UILabel!

}

1 Answers1

0

How did you connect your prototype cell's controls to the IBOutlets in you historyCell class ? I didn't know that was possible when you're not using a UITableviewController. I've been using tags all this time.

Unless there's something new that I wasn't aware of, your assignment to cell.durationLabel.text should cause unwrapping of a nil (is that the error you're getting ?)

Alain T.
  • 40,517
  • 4
  • 31
  • 51
  • Yeah thats the error i used to get I would run into a nil unwrapping. I think i got it to display now, but it only displays my label after i rotate my screen, weird... What you mean by how i connected it ? @IBOutlet var durationLabel: UILabel! – Triscuit Delicious Jan 17 '16 at 16:42
  • You just confirmed something I was suspecting while reading your question. When I started working with UITableViews, the only way I could access the custom cells' controls was through findViewWithTag. After like 3 versions of XCode I never went back to find a better way. You made me realize that there's a way to connect table view cells to IBOutlets somehow. I'm gonna check that out and probably eliminate a lot of code. And I love eliminating code. Thanks. – Alain T. Jan 17 '16 at 19:18
  • When you rotate the screen, a re-paint, and probably a new call to cellForRowAtIndexPath is made. Did you check that cellForRowAtIndexPath is called at least once when the view initially appears ? If it is not called, that would mean that the table view thinks the prototype cell from design time is usable as-is (which would be weird indeed). If it is called, then I'll need to do my homework on IBOutlets in UITableViewCells before I can even pretend to be helpful. – Alain T. Jan 17 '16 at 19:37
  • My tableviews were programmed when we still needed to call tableView.reload() explicitly (now setting the delegate is supposed to handle everything). Perhaps there are still cases where calling .reload() is needed. I would suggest doing that. at the very least it should force the tableview to redraw cells. – Alain T. Jan 23 '16 at 01:32
  • I'm stumped as well. Just one last thing. would it be possible that the background color you're setting for the view is so dark that you can't see cell content that is in fact showing properly ? – Alain T. Jan 23 '16 at 03:29
  • Unchecking "Use Size Classes" worked for me... idk what this is but after i turned it back on it still works... i guess its a bug – Triscuit Delicious Jan 23 '16 at 03:44
  • http://stackoverflow.com/questions/24170922/creating-custom-tableview-cells-in-swift – Triscuit Delicious Jan 23 '16 at 03:44