当数据来自网络时重用 UIViewController 的文件管理器
File manager with reuse of UIViewController when the data come from the network
请帮忙!我正在尝试实现一个与 Web 服务器一起使用的文件管理器我有一个 UITableView 和来自 .xib 的自定义单元格,我正在使用此代码注册单元格
self.tableView.registerNib (UINib (nibName: MaterialCell, bundle: nil), forCellReuseIdentifier: MaterialCell)
我有相同的数据,所以我想再次使用 UIViewController 并在 didSelectRowAtIndexPath
此处写入此代码
var viewController = FloderViewController()
self.navigationController! .pushViewController`
问题是我从网络获取数据,当我尝试重新使用 UIViewController 时出现错误
fatal error: unexpectedly found nil while unwrapping an Optional value
我做错了什么,我该如何实施!谢谢你的帮助
编辑:
This my code
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)")
})
}
这一行
var viewController = FloderViewController()
将创建您的 FloderViewController
的实例,但它不会链接到故事板或 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)
请帮忙!我正在尝试实现一个与 Web 服务器一起使用的文件管理器我有一个 UITableView 和来自 .xib 的自定义单元格,我正在使用此代码注册单元格
self.tableView.registerNib (UINib (nibName: MaterialCell, bundle: nil), forCellReuseIdentifier: MaterialCell)
我有相同的数据,所以我想再次使用 UIViewController 并在 didSelectRowAtIndexPath
此处写入此代码
var viewController = FloderViewController() self.navigationController! .pushViewController`
问题是我从网络获取数据,当我尝试重新使用 UIViewController 时出现错误
fatal error: unexpectedly found nil while unwrapping an Optional value
我做错了什么,我该如何实施!谢谢你的帮助
编辑:
This my code
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)")
})
}
这一行
var viewController = FloderViewController()
将创建您的 FloderViewController
的实例,但它不会链接到故事板或 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)