gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:14:15 28 4
gpt4 key购买 nike

我有一个名为 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com