4
votes

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
    }
}
3
Is it crashing on tableview ?Reshmi Majumder
check your cell reuse identifier specified in the .storyboard, Xib, or in code, and ensure that it is correct when dequeuingRonak Chaniyara
Identifies is correct, I've double checked. It does crash when when I open this view. If I delete .delegate and .dataSource - no crash, but not cells as well.Xernox

3 Answers

5
votes

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...

2
votes

Check the following:

check your cell reuse identifier specified in the .storyboard, Xib, or in code, and ensure that it is correct when dequeuing.

otherwise it will give fatal error, the app crashes.

1
votes

You have declared the tableView as implicitly unwrapped. That means this has to be initialized before assigning value to this. I guess you have just declared the tableView but not wired to the story board.

I you do not want that way declare it as optional and initialize it before setting the datasource and delegate.