Mike Schmidt Mike Schmidt - 4 months ago 46
Swift Question

Reloading Table Data in Swift?

I have a class called

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

When I test the variable
MainTableView
in the
ViewDidLoad
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
tableView
instead of
MainTableView
(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!

names.append(Name)
descriptions.append(Description)
categories.append(Category)
types.append(Type)
series.append(Series)
groups.append(Group)

}


}



func showTags(showTags: Bool) {

tableView.reloadData()
}



func showTimestamp(showTimeStamp: Bool) {
tableView.reloadData()
}


override func viewDidLoad() {
super.viewDidLoad()

parseJSON()



//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() {
super.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
}
else{
cell.tag1.hidden = true
}
if showTimeStampVal{
cell.tag2.hidden = false
}
else{
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

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.

Comments