Swift:tableView总是“发现零,而unwr ...”在视图控制器内

问题描述:

在一个viewController我已经连接了SB中的tableView outlet与委托和数据源集,当我访问代码中的参考插座时,它总是崩溃与消息:Swift:tableView总是“发现零,而unwr ...”在视图控制器内

fatal error: unexpectedly found nil while unwrapping an Optional value

下面的代码:

@IBOutlet var resultTabView: UITableView! 

连接:

enter image description here

cellForRowAtIndexPath: -
我不即使使用resultsArray,虽然如在日志中看到的阵列被返回数据

编辑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> &amp; 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 
} 
+0

检查resultTabView数据源和委托连接 – Sofeda

+0

看到附加的图像 –

+0

在事故中可能无法TableView中,但数据,尽量去打印之前'reloadData'了''resultTabView,看看它的零或不 – Tj3n

按照以下步骤,

  1. 请删除与表视图所有连接并重新添加。

  2. 确保tableViewCellIdentifier在storyboard中的名称与代码中给出的名称相同。

  3. 删除“!” AnyObject后马克,我beleive你需要这个

    VAR resultsArray =(VAL为?[AnyObject])

  4. 这不是重装错误表视图,它的解析问题。请检查所有声明属性。如果可能,请显示所有表视图数据源方法。

确保在尝试访问表视图之前调用viewDidLoad方法。您的来自XIB的连接可能不会在此代码运行时建立。

+0

加载视图控制器时,它不是第一件被调用的东西吗? –

+0

_什么意思_“加载视图控制器”_?分配它?不是。第一次访问'.view'属性时,XIB加载IBOutlet连接。访问任何其他IBOutlet属性将不会加载XIB。 – Tricertops

+0

你可以看到我编辑的问题,现在显示整个班级 –