2016-10-14 2 views
-1

테이블 뷰에 데이터를로드하는 데 문제가 있습니다. 데이터가 제대로로드되지 않는 것 같습니다. 난 그냥 IOS 프로그래밍을 시작하고,이 문제에 잠시 붙어있다.왜 이러한 코드가 포함 된 빈 Tableview가 생깁니 까? JSON

Here is a screenshot of my storyboard

override func viewDidLoad() { 
    super.viewDidLoad() 
} 

var afspraken = [Afspraak]() 

func loadJsonData() 
{ 
    let url = URL(string: "https://i342444.venus.fhict.nl/") 
    let dataTask = URLSession.shared.dataTask(with: url!) 
    { 
     (data, response, error) 
     in 
     if error != nil 
     { 
      print (error) 
     } 
     else 
     { 
      do 
      { 
       let jsonObject = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) 
       self.parseJsonData(jsonObject as! Array<AnyObject>) 
       print(data) 
      } 
      catch let error as NSError {print("Error parsing JSON: \(error)")} 
     } 
    } 

    dataTask.resume() 
} 

func parseJsonData(_ jsonObject: Array<AnyObject>) { 
    for item in jsonObject 
    { 
     let afspraak = 
      Afspraak(naam: item["naam"] as! String 
       , adres: item["adres"] as! String 
       , postcode: item["postcode"] as! String 
       , woonplaats: item["woonplaats"] as! String 
       , telefoonnummer: item["telefoonnummer"] as! String 
       , email: item["email"] as! String 
       , datum: item["datum"] as! Date 
       , tijdstip: item["tijdstip"] as! String 
       , omschrijving: item["omschrijving"] as! String) 


     afspraken.append(afspraak) 
    } 
    self.tableView.reloadData() 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return afspraken.count 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    let Cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 
    let currentRow = indexPath.row 
    let currentAfspraak = self.afspraken[currentRow] 
    Cell.textLabel?.text = currentAfspraak.naam 
    return Cell 
} 

답변

0

URLDataTask 요청에 배경 스레드 (스레드 비 UI)에서 실행된다. 따라서 완료 후 GCD-Grand Central Dispatch를 사용하여 메인 스레드의 UI를 업데이트해야합니다.

DispatchQueue.main.sync { 
    self.tableView.reloadData() 
} 

내가 당신의 JSON의 URL에보고했다

편집, 그것은 HTML 파일입니다하는 JSON 파일이 아닙니다. 그렇기 때문에 JSONSerialization이 무효로 반환됩니다. json string을 HTML 파일에 임베드하는 대신 서버에 .json 파일을 넣어야합니다.

서버에있는 파일은 다음과 같다 : 당신이 볼 수 as you can see this is an HTML.

이는 HTML입니다.

올바른 JSON 파일은 다음과 같이해야한다 : enter image description here

이미지의 상단에있는 두 개의 URL의 차이를주의하십시오, 올바른 사람은 .json을 가지고 있으며, 당신은을이다, 단지 /입니다 예배 규칙서.

따라서 당신이해야 할 것은 당신의 서버에 JSON 파일을 업로드하고 진정한 https://i342444.venus.fhict.nl/coolName.json

+0

처럼, 해당 URL을 사용하는 것입니다, 이것은 내가 여전히 추가하기 위해 필요한 것이었다. 하지만 문제는 JSON 데이터를로드하는 것 같습니다. 코드가 실행되는 동안 배열이 비어있어 무언가가 잘못 될 수 있습니다. –

+0

@DennisvdEijnde 내 편집 된 답변을 참조하십시오. URL에 문제가 있습니다. –

관련 문제