Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error (found nil) while loading stock tableView

I made basic tableView inside ViewController and while loading I get

fatal error: unexpectedly found nil while unwrapping an Optional value

Which points to tableView.delegate = self (by points to I mean this line is highlighted in green colour in Xcode). Here's full code:import UIKit

import UIKit

class FAQViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UINavigationControllerDelegate, SWRevealViewControllerDelegate {

    @IBOutlet var menuButton: UIBarButtonItem!
    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self

        if revealViewController() != nil {
            //I have SWRevealController that slides viewController from Left side
        }        
    }

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

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("FAQ") as! FAQTableViewCell
        cell.questionLabel.text = "Here goes Question"
        cell.answearLabel.text = "This is answear"

        return cell
    }
}
like image 436
Xernox Avatar asked Feb 07 '23 16:02

Xernox


1 Answers

From what I can see from your example, you look to be setting things up using a storyboard, but since the class is a UIViewController and not a UITableViewController, I think your connections are not wired up correctly.

I would check in the debugger to make sure tableView is not nil and to check in the storyboard to make sure that the connections look OK.

You can also wire up the tableViews dataSource and delegate in storyboard by right clicking on the tableView, and then dragging from the circle across from dataSource or delegate to the view controller associated with your storyboard scene (i.e. the first icon in the hierarchy right below the scene name in the storyboard file).

Happy to clarify if this does not make sense...

like image 68
robowen5mac Avatar answered Feb 13 '23 06:02

robowen5mac