2016-11-09 2 views
1

Ich habe eine App, die Swift 3 und Alamofire verwendet. Die Daten sind mit zwei cell?.viewWithTag(2) und cell?.viewWithTag(1) verbunden, die ein Bild (von der URL) und Text sind. Wenn ich das Projekt starte, wird nichts in meiner App angezeigt. Ich habe den JSON mit print(readableJSON) getestet und der JSON wird in die Konsole gedruckt. Ich bin also ein wenig verwirrt. Mein swift sieht wie folgt aus:Nichts in der App von JSON URL mit Swift 3 und Alamofire

SWIFT

import UIKit 
import Alamofire 

struct postinput { 
    let mainImage : UIImage! 
    let name : String! 

} 


class TableViewController: UITableViewController { 

    var postsinput = [postinput]() 

    var mainURL = "https://www.example.api.com" 

    typealias JSONstandard = [String : AnyObject] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     callAlamo(url: mainURL) 
    } 

    func callAlamo(url : String){ 
     Alamofire.request(url).responseJSON(completionHandler: { 
      response in 

      self.parseData(JSONData: response.data!) 


     }) 

    } 

    func parseData(JSONData : Data) { 
     do { 
      var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONstandard 
     print(readableJSON) 

      if let posts = readableJSON["posts"] as? [JSONstandard] { 
       for post in posts { 
        let title = post["title"] as! String 
        print(title) 

        if let imageUrl = post["image"] as? JSONstandard { 
         let mainImageURL = URL(string: imageUrl["url"] as! String) 
         let mainImageData = NSData(contentsOf: mainImageURL!) 


         let mainImage = UIImage(data: mainImageData as! Data) 
         postsinput.append(postinput.init(mainImage: mainImage, name: title)) 
         self.tableView.reloadData() 

        } 


       } 

      } 

     } 


     catch { 
      print(error) 
     } 


    } 




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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 

     // cell?.textLabel?.text = titles[indexPath.row] 

     let mainImageView = cell?.viewWithTag(2) as! UIImageView 

     mainImageView.image = postsinput[indexPath.row].mainImage 

     let mainLabel = cell?.viewWithTag(1) as! UILabel 

     mainLabel.text = postsinput[indexPath.row].name 


     return cell! 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 

JSON

{ 
    "posts" : [{ 
     "id": "000000", 
     "url": "/content/interview2", 
     "date": "2016-11-03 09:01:41", 
     "modified": "2016-11-03 09:03:47", 
     "title": "An interview", 
     "image": "https://www.example.com/sites/default/files/oregood.jpeg", 
     "summary": { 
      "value": "<p>Latin text here</p>", 
      "format": "filtered_html" 
     } 
    }] 
} 
+0

Sie bekommen Jason, aber nicht Daten in der Tabellenansicht anzeigen, oder? – Mohit

+0

Korrekte Mohit .. – rob

+0

Ich habe nicht "NumberOfSectionsInTableView" -Methode, wo ist das? –

Antwort

1

Von Ihrer JSON-Antwort image Schlüssel enthalten String nicht Dictionary auch brauchen, um Ihre tableView außerhalb for-Schleife nachladen nicht jedes Mal in der Schleife, so versuchen, wie dies .

if let posts = readableJSON["posts"] as? [JSONstandard] { 
    for post in posts { 
     let title = post["title"] as! String    
     if let imageUrl = post["image"] as? String { 
      let mainImageURL = URL(string: imageUrl as! String) 
      let mainImageData = NSData(contentsOf: mainImageURL!) 
      let mainImage = UIImage(data: mainImageData as! Data) 
      postsinput.append(postinput.init(mainImage: mainImage, name: title))     
     } 
    } 
    DispatchQueue.main.async { 
     self.tableView.reloadData() 
    } 
} 

Vorschlag: Stattdessen Bild des Herunterladens NSData(contentsOf:) auf Hauptthread Teig mit Bibliothek verwenden wie SDWebImages oder Sie können Ihren eigenen async Bild Downloader erstellen.

+0

Das hat Nirav funktioniert ... danke! – rob

0

, weil Sie nicht Ihre JSON in Tabellenzellen anzeigt. Sie Kiste ein kopiertes Objekt „mainImageView“ genannt und es nie als eigentliche Imageview von Tabellenzelle zugewiesen werden, stattdessen versuchen:

(cell?.viewWithTag(2) as? UIImageView).image = postsinput[indexPath.row].mainImage 

postsinput enthält Bildfolgen oder Bilder?

EDIT: so wäre es:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 

     // cell?.textLabel?.text = titles[indexPath.row] 

     //let mainImageView = cell?.viewWithTag(2) as! UIImageView 

     //mainImageView.image = postsinput[indexPath.row].mainImage 
     (cell?.viewWithTag(2) as? UIImageView).image = postsinput[indexPath.row].mainImage 

     //let mainLabel = cell?.viewWithTag(1) as! UILabel 

     //mainLabel.text = postsinput[indexPath.row].name 
     (cell?.viewWithTag(1) as? UILabel).text = postsinput[indexPath.row].name 

     return cell! 
    } 
+0

An welchem ​​Punkt würde dieser Code Repoguy gehen? – rob

+0

innerhalb Ihrer override-Funktion func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {}, statt let mainImageView = Zelle? .viewWithTag (2) as! UIImageView UND mainImageView.image = postsinput [indexPath.row] .mainImage Das gleiche gilt für Ihr Etikett – repoguy

+0

Vielen Dank, aber das Problem nicht gelöst. Aber es ist jetzt dank Repoguy gelöst – rob

Verwandte Themen