Swift:tableView 总是“在 unwr 时发现 nil...";在视图控制器内 [英] Swift: tableView always "found nil while unwr..." inside a viewcontroller
问题描述
在 viewController
中,我已经将 SB 中的 tableView outlet
与委托和数据源集连接起来,当我在代码中访问参考出口时,它总是崩溃消息:
In a viewController
I've connected a tableView outlet
in the SB with delegates and datasource set and when I'm accessing the reference outlet in code it always crashes with the message:
致命错误:解包可选值时意外发现 nil
fatal error: unexpectedly found nil while unwrapping an Optional value
代码如下:
@IBOutlet var resultTabView: UITableView!
连接:
cellForRowAtIndexPath:
-
我什至没有使用 resultsArray,尽管该数组正在返回日志中所见的数据
cellForRowAtIndexPath:
-
I'm not even using resultsArray, though the array is returning data as seen in the logs
编辑 2:
数据:
id = 2325;
results = (
{
city = Delhi;
company = "AIR CREATOR";
country = IN;
date = "Mon, 11 Jul 2016 04:51:34 GMT";
expired = 0;
formattedLocation = "Delhi, Delhi";
formattedLocationFull = "Delhi, Delhi";
formattedRelativeTime = "30+ days ago";
jobkey = 67ba422cc328a946;
jobtitle = "CSA for airport";
latitude = "28.664835";
longitude = "77.21429000000001";
onmousedown = "indeed_clk(this,'3586');";
snippet = "Providing boarding passes and luggage <b>labels</b>. Your daily tasks will usually include:....";
source = Indeed;
sponsored = 0;
state = DL;
url = "http://www.indeed.co.in/viewjob?jk=67ba422cc328a946&qd=SHSUhPNSBYpqaNu9AVdfqv81wDpAwFE36uyNVXcxl8VAUThqIJkZA1l3w1yQl8AJg05AJq_aODrwRlOUKXT92UBk4SDqDqM7LhTReglYsV0&indpubnum=4148406673078092&atk=1aqbfaj6kb958c17";
},
{
city = Delhi;
company = "Letmedrive India Private Limited";
country = IN;
date = "Wed, 10 Aug 2016 13:02:51 GMT";
expired = 0;
formattedLocation = "Delhi, Delhi";
formattedLocationFull = "Delhi, Delhi";
formattedRelativeTime = "6 days ago";
jobkey = 89c4f63f1b841720;
jobtitle = "Graphics Designer";
latitude = "28.664835";
longitude = "77.21429000000001";
onmousedown = "indeed_clk(this,'3586');";
snippet = "Graphic designers/artists design graphics for use in media products such as magazines, <b>labels</b> & advertising....";
source = Indeed;
sponsored = 0;
state = DL;
url = "http://www.indeed.co.in/viewjob?jk=89c4f63f1b841720&qd=SHSUhPNSBYpqaNu9AVdfqv81wDpAwFE36uyNVXcxl8VAUThqIJkZA1l3w1yQl8AJg05AJq_aODrwRlOUKXT92UBk4SDqDqM7LhTReglYsV0&indpubnum=4148406673078092&atk=1aqbfaj6kb958c17";
};
班级:
class JobListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate{
@IBOutlet var searchField: UITextField!
@IBOutlet var resultTabView: UITableView!
var resultsArray = [JSON]()
var resultsDictionary = [String:String]()
override func viewDidLoad() {
super.viewDidLoad()
}
func focusSearchField(search:Bool, keyword:String){
if(search){
callAPI(keyword)
}else{
searchField?.becomeFirstResponder()
}
}
func callAPI(keyword:String){
var params = [String:String]()
params["publisher"] = "2341411"
params["q"] = keyword
params["l"] = "dubai"
params["latlong"] = "1"
params["v"] = "2"
params["format"] = "json"
params["co"] = "uae"
let svcMgr = ServiceManager()
svcMgr.httpRequest(self, params: params)
}
func updateUI(result: [String:AnyObject]){
if let val = result["results"] {
let json = JSON(val)
let array = json.arrayValue
dispatch_async(dispatch_get_main_queue(), {
for a in array{
self.resultsArray.append(a)
}
self.resultTabView.reloadData()
})
}else{
}
}
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return resultsArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("JobCell", forIndexPath: indexPath) as UITableViewCell
let labl2 = cell.viewWithTag(2) as! UILabel
labl2.text = (resultsArray[indexPath.row]["jobtitle"].stringValue)+" @"+(resultsArray[indexPath.row]["source"].stringValue)
let labl3 = cell.viewWithTag(3) as! UILabel
labl3.text = (resultsArray[indexPath.row]["state"].stringValue)+", "+(resultsArray[indexPath.row]["country"].stringValue)
let labl4 = cell.viewWithTag(4) as! UILabel
labl4.text = (resultsArray[indexPath.row]["snippet"].stringValue)+" "
tableView.estimatedRowHeight = 80
tableView.rowHeight = UITableViewAutomaticDimension
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
UIApplication.sharedApplication().openURL(NSURL(string: resultsArray[indexPath.row]["url"].stringValue)!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - TextField delegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
callAPI(textField.text!)
return true
}
推荐答案
确保在尝试访问表视图之前调用了 viewDidLoad
方法.在此代码运行时,您与 XIB 的连接可能尚未建立.
Make sure the viewDidLoad
method is called before attempting to access the table view. Your connection from XIB is probably not established by the time this code runs.
这篇关于Swift:tableView 总是“在 unwr 时发现 nil...";在视图控制器内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!