请帮忙!我正在尝试实现一个与 Web 服务器一起使用的文件管理器
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)")
})
}