请帮忙!我正在尝试实现一个与Web服务器一起工作的文件管理器我有一个带有来自.xib的自定义单元的UITableView,我正在使用此代码来注册单元格
self.tableView.registerNib (UINib (nibName: MaterialCell, bundle: nil), forCellReuseIdentifier: MaterialCell)
我有相同的数据,所以我想再次使用UIViewController并在didSelectRowAtIndexPath
中写入此代码
var viewController = FloderViewController() self.navigationController! .pushViewController`
问题是我从网络获取数据,当我尝试重新使用UIViewController时出现错误
致命错误:在解包可选值时意外发现nil
我做错了什么,我该如何实现呢!谢谢你的帮助
编辑:
这是我的代码
import UIKit
class FloderViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let Queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)
let Main = dispatch_get_main_queue()
let CustomCell = "CustomCell"
//MARK: - Variables -
var elements: Array<JSON> = []
//MARK: - IBOutlet -
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
getJSON("params", value: params!)
self.tableView.registerNib(UINib(nibName: CustomCell, bundle: nil), forCellReuseIdentifier: CustomCell)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return elements.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(CustomCell, forIndexPath: indexPath) as CustomCellClass
cell.name.text = elements[indexPath.row]["name"].stringValue
cell.autor.text = elements[indexPath.row]["autor"].stringValue
cell.date.text = elements[indexPath.row]["date"].stringValue
return cell
}
//MARK: - UITableViewDelegate -
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
params = materials[indexPath.row]["folderPath"].stringValue
var viewController = FloderViewController()
self.navigationController!.pushViewController(viewController, animated: true)
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100
}
func getJSON (key: NSString, value: AnyObject) {
var request = HTTPTask()
request.baseURL = BaseURL
let params: Dictionary<String,AnyObject> = [key: value]
var error: NSError?
request.POST(OpenFolder, parameters: params, success: {(response: HTTPResponse) in
if let data = response.responseObject as? NSData {
var responseBeltJSON: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &error)
var json = JSON(responseBeltJSON!)
elements = json["elements"].arrayValue
dispatch_async(self.Main, {
self.tableView.reloadData()
})
}
},failure: {(error: NSError, response: HTTPResponse?) in
println("error: \(error)")
})
}
答案 0 :(得分:2)
这一行
var viewController = FloderViewController()
将创建FloderViewController
的实例,但它没有链接到Storyboard或nib文件,因此除非您以编程方式创建所有UI元素(包括tableView
) IBOutlets
将为零 - 正如您所见。
如果您正在使用故事板,那么您应该像这样创建视图控制器 -
let viewController = self.storyboard.instantiateViewControllerWithIdentifier("floderVCId") as FloderViewController // Identifier must match the scene identifier in the storyboard
self.presentViewController(viewController, animated: true, completion: nil)