2016-04-26 29 views
0

我有一個集合視圖將水平滾動。並在該集合視圖中每個單元格點擊 - 我有加載表視圖中的相應數據。因此,我將我的表視圖放在集合視圖下。應用程序崩潰同時運行集合視圖和表視圖同時.Crash:致命錯誤:意外地發現零,同時展開一個可選值

崩潰報告:

fatal error: unexpectedly found nil while unwrapping an Optional value 

這裏是我的代碼:

class HomeDiscoverViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet var BTCollectionView: UICollectionView! 

    @IBOutlet var DLTableView: UITableView! 

    var BTdata = [BTData]() 

    var Dealsdata = [DealsData]() 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 
     ListBusinessTypes() 
    } 
// Values from Api for Business Types 
    func ListBusinessTypes() 
    { 
     let token = NSUserDefaults.standardUserDefaults().valueForKey("access_token") as! String 

     let headers = ["x-access-token": token] 

     let request = NSMutableURLRequest(URL: NSURL(string: "someurl")!, 
              cachePolicy: .UseProtocolCachePolicy, 
              timeoutInterval: 10.0) 
     request.HTTPMethod = "GET" 
     request.allHTTPHeaderFields = headers 

     let session = NSURLSession.sharedSession() 
     let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in 
      if (error != nil) 
      { 
       print(error) 

       let ErrorAlert = UIAlertController(title: "Error", message: "Problem with internet connectivity or server, please try after some time", preferredStyle: UIAlertControllerStyle.Alert) 

       // add an action (button) 
       ErrorAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 

       // show the alert 
       self.presentViewController(ErrorAlert, animated: true, completion: nil) 
      } 
      else 
      { 
       if let json = (try? NSJSONSerialization.JSONObjectWithData(data!, options: [])) as? Dictionary<String,AnyObject> 
       { 
        let success = json["success"] as? Int 

        if(success == 1) 
        { 
         if let typeValues = json["data"] as? [NSDictionary] 
         { 
          dispatch_async(dispatch_get_main_queue(),{ 

           for item in typeValues 
           { 
            self.BTdata.append(BTData(json:item)) 
           } 
          }) 
         } 
         self.BTCollectionView.reloadData() 
        } 
        else 
        { 
         let message = json["message"] as? String 

         print(message) 

         let ServerAlert = UIAlertController(title: "Error", message: message, preferredStyle: UIAlertControllerStyle.Alert) 

         // add an action (button) 
         ServerAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 

         // show the alert 
         self.presentViewController(ServerAlert, animated: true, completion: nil) 
        } 
       } 
      } 
     }) 

     dataTask.resume() 
    } 

    // values from Api For Deals 
    func ListDeals(BTId:String) 
    { 
     let token = NSUserDefaults.standardUserDefaults().valueForKey("access_token") as! String 

     let headers = [ 
      "cache-control": "no-cache", 
      "postman-token": "1befe4c6-ec7c-ccb3-f537-97307f451807" 
     ] 

     let StringURL = "someurl"+token 

     let request = NSMutableURLRequest(URL: NSURL(string: StringURL)!, 
              cachePolicy: .UseProtocolCachePolicy, 
              timeoutInterval: 10.0) 
     request.HTTPMethod = "GET" 
     request.allHTTPHeaderFields = headers 

     let session = NSURLSession.sharedSession() 
     let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in 
      if (error != nil) 
      { 
       print(error) 
      } 
      else 
      { 
       if let json = (try? NSJSONSerialization.JSONObjectWithData(data!, options: [])) as? Dictionary<String,AnyObject> 
       { 
        let success = json["success"] as? Int 

        if(success == 1) 
        { 
         if let DealsValues = json["data"] as? [NSDictionary] 
         { 
          dispatch_async(dispatch_get_main_queue(),{ 

           for item in DealsValues 
           { 
            let itemObj = item as? Dictionary<String,AnyObject> 

            let b_type = itemObj!["business_type"] as? String 

            if(b_type == BTId) 
            { 
             self.Dealsdata.append(DealsData(json:item)) 
            } 
           } 
           self.DLTableView.reloadData() 
          }) 
         } 
        } 
        else 
        { 
         let message = json["message"] as? String 

         print(message) 

         let ServerAlert = UIAlertController(title: "Error", message: message, preferredStyle: UIAlertControllerStyle.Alert) 

         // add an action (button) 
         ServerAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 

         // show the alert 
         self.presentViewController(ServerAlert, animated: true, completion: nil) 
        } 

       } 

      } 
     }) 

     dataTask.resume() 
    } 

    // Mark : Collection View Delegate and Datasource(Business Type) 
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
    { 
     return BTdata.count 
    } 

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 
    { 
     let cell: DDLCollectionCell = collectionView.dequeueReusableCellWithReuseIdentifier("BTCell", forIndexPath: indexPath) as! DDLCollectionCell 
     cell.BTName.text = BTdata[indexPath.row].BTNames 
     return cell 
    } 

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 
    { 
     ListDeals(BTdata[indexPath.row].BTIds!) 
    } 



    // Mark : Table View Delegate and Datasource(Deals) 

    // count of search value and row value that are displaying 
    func numberOfSectionsInTableView(tableView: UITableView) -> Int 
    { 
     return self.Dealsdata.count 
    } 

    // number of rows 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return 1 
    } 


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cell:DealsListTableCell = self.DLTableView.dequeueReusableCellWithIdentifier("cell") as! DealsListTableCell 
     cell.DealName.text = Dealsdata[indexPath.row].DealNames 
     cell.DealExpiryDate.text = Dealsdata[indexPath.row].DealAddresses 
     return cell 
    } 

我得到的是fatel錯誤的位置:

self.BTCollectionView.reloadData() 

ListBusinessTypes() method.I添加的所有委託和數據源。但不知道什麼是ia失蹤。請幫助我。

謝謝。

+0

outlet'BTCollectionView'已連接? –

+0

是的,它的連接 – user5513630

回答

0

這可能發生或者兩個原因:

  1. You may not given the right "Reuse Identifier" (in your case: either 'BTCell' or 'cell') in the cell class xib of either table view or collection view.

  2. If mistakenly you are setting "nil" text to label.

Generally this type of error comes when your variable is set to nil, but your code is expecting it to not be nil (try to figure out this by using the error breakpoints).

希望這將幫助您解決!

+0

我第一次只檢查了這兩件事。但事情很好。但我不知道我給它在帽子集合視圖名稱中的這個錯誤?如果我添加集合視圖名稱.delegate&數據源=自我也。然後,如果我運行。再次收集視圖 – user5513630

+0

的數據源中,我收到了這個崩潰,這是否只發生在集合視圖? –

+0

是的。我刪除了我的表格視圖,我檢查了。那時同樣的錯誤在集合視圖 – user5513630

相關問題