Swift:tableView总是"发现nil而unwr ......"在viewcontroller中

时间:2016-08-17 05:00:25

标签: ios swift uitableview

viewController我已将SB中的tableView outlet与代理和数据源集相关联,当我在代码中访问参考插座时,它总是与消息崩溃:

  

致命错误:在解包可选值时意外发现nil

以下是代码:

@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
}

2 个答案:

答案 0 :(得分:0)

按照以下步骤

  1. 请删除与表格视图的所有连接,然后重新添加。

  2. 确保tableViewCellIdentifier在故事板中设置的名称与代码中的名称相同。

  3. 删除&#34;!&#34;在AnyObject之后标记,我相信你需要这个

    var resultsArray =(val as?[AnyObject])

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

答案 1 :(得分:0)

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

相关问题