2017-02-18 3 views
1

Ich versuche gerade, die JSON-Daten in meinem UITableView anzuzeigen. Ich habe eine separate Klasse erstellt, die die Daten behandelt und deklariert, die aus der API extrahiert werden. Ich habe auch eine benutzerdefinierte Zelle erstellt, um die Daten anzuzeigen. Ich bin mir jedoch nicht sicher, wie ich die Bilder der API anzeigen soll. Ich konnte den Titel der API nur einen Standard-Tableviewcontroller ohne individuelle Zellen unter Verwendung anzuzeigen:JSON imageURL mit Alamofire und SwiftyJSON in UITableView anzeigen

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

    cell.textLabel?.text = recipes = [indexPath.row].title 

    return cell 
} 

aber bei dem Versuch, eine Gewohnheit zu schaffen die Bild Zelle verwendet eine imageURL, die anders funktioniert. Andere Tutorials und Demo scheinen nicht zu verwenden Alamofire und SwiftyJSON.

habe ich die Dokumentation zu lesen: https://github.com/SwiftyJSON/SwiftyJSON#integration aber sicher noch nicht, wie dieses Problem zu lösen. Bitte sagen Sie mir, wie Sie die API-Daten auf meinem Tisch anzeigen lassen können.

Klasse

import Foundation 
import SwiftyJSON 

    class Recipe { 

     var title: String! 
     var image: URL? 


     init(json: JSON) { 
      self.title = json["title"].stringValue 
      self.image = json["image"].url 

     } 


    } 

CustomCell

import UIKit 

class RecipeCell: UITableViewCell { 


    @IBOutlet weak var titleLabel: UILabel! 
    @IBOutlet weak var imgView: UIImageView? 



    override func awakeFromNib() { 
     super.awakeFromNib() 

     // Initialization code 
    } 

    override func setSelected(_ selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

} 

Viewcontroller

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view, typically from a nib. 
    Alamofire.request(searchURL, method: .get, parameters: params, encoding: URLEncoding.default, headers: headers).response { [unowned self] response in 
     guard let data = response.data else { return } 
     let json = JSON(data: data) 

     for recipe in json.arrayValue { 
      let newRecipe = Recipe(json: recipe) 
      self.recipes.append(newRecipe) 


     } 

     for recipe in self.recipes { 
      print(recipe.title) 
      print(recipe.image) 

     } 



    } 


} 

// Display JSON Data into the tableView 

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

} 

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

    // What to write here? 

    return cell 
} 

Vielen Dank im Voraus! :)

Antwort

0

hierfür müssen Sie die Erweiterung von Imageview machen als

extension UIImageView { 
    func downloadedFrom(url: URL, contentMode mode: UIViewContentMode = .scaleAspectFit) { 
     contentMode = mode 
     URLSession.shared.dataTask(with: url) { (data, response, error) in 
      guard 
       let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200, 
       let mimeType = response?.mimeType, mimeType.hasPrefix("image"), 
       let data = data, error == nil, 
       let image = UIImage(data: data) 
       else { return } 
      DispatchQueue.main.async() {() -> Void in 
       self.image = image 
      } 
      }.resume() 
    } 
    func downloadedFrom(link: String, contentMode mode: UIViewContentMode = .scaleAspectFit) { 
     guard let url = URL(string: link) else { return } 
     downloadedFrom(url: url, contentMode: mode) 
    } 
} 

dann müssen Sie cellForRowAt Methode schreiben als

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "recipeCell", for: indexPath) 
    let recipe = recipes[indexPath.row] 
    let imagePath = recipe.image 
    cell.titleLabel.text = recipe.title 
    cell. imgView.downloadedFrom(link: imagePath) 

     return cell 
    } 
+0

vergessen Sie nicht, die Tabelle neu zu laden, wenn Sie Antwort erhalten von Server – Ram

+0

Danke, ich war in der Lage, die Erweiterung zu verwenden und die ImageURL in der UIImageView erkannt werden. – Chace

Verwandte Themen