3

请帮忙!我正在尝试实现一个与 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)")
        })
    }
4

1 回答 1

2

这条线

var viewController = FloderViewController ()

将创建您的实例,FloderViewController但它没有链接到 Storyboard 或 nib 文件,因此除非您以tableView编程方式创建所有 UI 元素(包括您的 ),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)
于 2015-01-24T11:59:25.903 回答