我有一个名为MainTableViewController的类和一个名为IBOutletMainTableView。当我尝试在委托函数中重新加载表视图时,出现了在控制台EXC_BAD_INSTRUCTION中读取的Fatal Error: found nil while unwrapping an optional value错误。

当我在类的MainTableView方法中测试变量ViewDidLoad时,它不是nil。但是,当我在委托函数中对其进行测试时,它为nil。

我在这里做错了什么?为什么不能使用出口将数据重新加载到表格视图中?

即使当我尝试使用tableView而不是MainTableView(我的插座)时,我也会遇到很多错误,因此绝对不能。

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)

    }

}


}

最佳答案

由于您的MainTableViewControllerUITableViewController的子类,因此它已经链接了内置表视图。尝试删除MainTableView插座(以及在界面生成器中所做的所有操作),并且每当需要访问表视图时,都使用从tableView继承的UITableViewController属性。

关于ios - 在Swift中重新加载表数据?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38417362/

10-11 07:54