ios - 在 Swift 中重新加载表数据?

标签 ios swift xcode uitableview

我有一个名为 MainTableViewController 的类,以及一个名为 MainTableViewIBOutlet。当我尝试在委托(delegate)函数中重新加载 TableView 时,我收到一个 EXC_BAD_INSTRUCTION 错误,该错误在控制台中读取 fatal error :在展开可选值时发现 nil

当我在类的 ViewDidLoad 方法中测试变量 MainTableView 时,它不是nil。但是,当我在委托(delegate)函数内测试它时,它是nil

我在这里做错了什么?为什么我无法使用 TableView 的导出重新加载表数据?

即使当我尝试使用 tableView 而不是 MainTableView (我的 socket )时,我也会收到大量错误,因此情况并非如此。

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的子类,因此它已经链接了内置表格 View 。尝试删除 MainTableView 导出(以及您在界面生成器中使用它所做的所有操作),并且每当您需要访问 TableView 时,请使用从 继承的 tableView 属性UITableViewController.

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

相关文章:

swift - Facebook ios sdk 不跟踪标准购买事件

ios - RestKit 核心数据映射一对多

ios - 将静态库链接到 iPhone 应用程序

swift - 无法更改 iOS 11 中 UINavigationBar 的高度

ios - 如何在 nginx 中设置前向保密,以便具有默认 ATS 设置的 iOS9 (Xcode7) 应用程序可以连接到我的服务器?

swift - 无法使用类型为 'swapAt' 的参数列表调用 '(Int, Int)'

ios - 是否可以在 ViewController 文件之外设置约束和对象?

iphone - 当 iPhone 中的 Appstore 上的应用程序版本更改时,sqlite 数据库更新

iphone - 将 NSDate 转换为 mm/dd/yyyy

ios - 为什么 NSBundle bundleIdentifier 是可选的?