2016-02-12 97 views
-1

我有一個UIViewController與ContainerView(連接到一個空白的UIView)和一個表。有時它顯示正確(我在viewDidLoad中加載JSON數據),但有時整個視圖都是黑色的。我試圖將其定位爲某種佈局更改,因此我將這些更改逆轉(視圖再次正確顯示),然後像以前一樣重新進行更改(查看仍然正確顯示)。UIViewController偶爾黑

當視圖爲黑色時,沒有崩潰,也沒有任何錯誤消息。該程序似乎仍然在後臺運行,我可以切換到TabBar的其他部分。

在代碼中出現了錯誤是唯一的指示,即

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

永遠不會被調用。但是,如果TableView失敗,爲什麼整個ViewController不顯示?

編輯:代碼viewDidLoad中。

let tURL = NSURL(string: "<urlofjsonfile>") 
    var JSONData:NSData? = nil 
    do { 
     JSONData = try NSData(contentsOfURL: tURL!, options: []) 
    } catch { 
     print("\(error)") 
    } 
    if let json = (try? NSJSONSerialization.JSONObjectWithData(JSONData!, options: [])) as? NSDictionary { 
     if let serienstartJsonArray = json["sendungen"] as? [NSDictionary] { 
      for item in serienstartJsonArray { 
       let meldung=SerienDataModel(json: item) 
       seriendaten.append(meldung) 
      } 
      arrSectionStart.append(0) 
      if let id = json["anzahl1"] as? Int { 
       arrSectionRows.append(id+1) 
      } else { 
       arrSectionRows.append(0) 
      } 
      if let numb2 = json["anzahl2"] as? Int { 
       arrSectionStart.append(arrSectionRows[0]+1) 
       arrSectionRows.append(numb2) 
      } else { 
       arrSectionRows.append(0) 
       arrSectionStart.append(1) 
      } 
      arrSectionStart.append(arrSectionRows[0]+arrSectionRows[1]+3) 
      if let numb3 = json["anzahl3"] as? Int { 
       arrSectionRows.append(numb3) 
      } else { 
       arrSectionRows.append(0) 
      } 

     } else { 
      print("Error creating dictionary") 
     } 
    } else { 
     print("Error NSJSONSerialization") 
    } 
+0

你能顯示代碼爲viewDidload – Woodstock

+0

我添加了代碼。 – Matt

+0

是你的tableview黑色的背景顏色? – uchiha

回答

0

您在viewDidLoad同步聯網:

JSONData = try NSData(contentsOfURL: tURL!, options: []) 

永遠永遠永遠做到這一點。除了後臺線程之外,不要同步網絡。網絡正確(異步),然後在主線程上更新您的模型和接口。

+0

你是對的 - 但即使我禁用整個網絡代碼(實質上是創建一個空表),視圖仍然是黑色的。它也不能解釋爲什麼整個視圖是黑色的,而不僅僅是表格視圖。 – Matt