Mike Schmidt Mike Schmidt - 1 year ago 174
Swift Question

Reloading Table Data in Swift?

I have a class called

, and an
. When I am trying to reload the table view in a delegate function, I am getting a
error that reads in the console
Fatal Error: found nil while unwrapping an optional value

When I test the variable
in the
method of my class, it is not nil. However, when I test it inside of my delegate function, it is nil.

What am I doing wrong here? Why can I not reload the table data using the outlet to the table view?

Even When I try to use
instead of
(my outlet), I get tons of errors, so that must not be the case.

import UIKit
import SwiftyJSON

class MainTableViewController: UITableViewController, SettingsSidebarViewDelegate {

@IBOutlet var settings: UIBarButtonItem!

@IBOutlet var MainTableView: UITableView!

var NumberofRows = 0
var names = [String]()
var descriptions = [String]()
var categories = [String]()
var types = [String]()
var series = [String]()
var groups = [String]()

func parseJSON(){
let path = NSBundle.mainBundle().URLForResource("documents", withExtension: "json")

let data = NSData(contentsOfURL: path!) as NSData!

let readableJSON = JSON(data: data)

NumberofRows = readableJSON["Documents"].count

for i in 1...NumberofRows {
let doc = "Doc" + "\(i)"
let Name = readableJSON["Documents"][doc]["name"].string as String!
let Description = readableJSON["Documents"][doc]["description"].string as String!
let Category = readableJSON["Documents"][doc]["category"].string as String!
let Type = readableJSON["Documents"][doc]["type"].string as String!
let Series = readableJSON["Documents"][doc]["tags"]["series"].string as String!
let Group = readableJSON["Documents"][doc]["tags"]["group"].string as String!




func showTags(showTags: Bool) {


func showTimestamp(showTimeStamp: Bool) {

override func viewDidLoad() {


//Sets button title to gear, sets button actions (to open menu)
settings.title = NSString(string: "\u{2699}\u{0000FE0E}") as String!
let font = UIFont.systemFontOfSize(25);
settings.setTitleTextAttributes([NSFontAttributeName: font], forState:UIControlState.Normal)
settings.target = self.revealViewController()
settings.action = #selector(SWRevealViewController.rightRevealToggle(_:))


override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

// MARK: - Table view data source

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

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return NumberofRows

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

let cell = tableView.dequeueReusableCellWithIdentifier("MainTableCell", forIndexPath: indexPath) as! MainTableViewCell

if names.count != 0{
cell.fileName.text = names[indexPath.row]
cell.fileDescription.text = descriptions[indexPath.row]
cell.fileCategory.text = categories[indexPath.row]
cell.fileType.text = types[indexPath.row]

cell.options.setTitle(NSString(string: ":") as String!, forState: .Normal)

cell.tag1.text = series[indexPath.row]
cell.tag2.text = groups[indexPath.row]

if showTagsVal{
cell.tag1.hidden = false
cell.tag1.hidden = true
if showTimeStampVal{
cell.tag2.hidden = false
cell.tag2.hidden = true


return cell

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("showView", sender: self)

// MARK: - Navigation

//In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "showView"){
let detailVC: DetailViewController = segue.destinationViewController as! DetailViewController
let indexPath = self.MainTableView.indexPathForSelectedRow!
detailVC.text = names[indexPath.row]

self.MainTableView.deselectRowAtIndexPath(indexPath, animated: true)




Answer Source

As your MainTableViewController is subclass of UITableViewController, it has linked the built-in table view already. Try to remove the MainTableView outlet (and everything you did with it in interface builder), and whenever you need to access the table view, use tableView property inherited from UITableViewController.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download