2017-09-03 1 views
0

Le problème est ----> Le TableView Afficher le même titre et la distribution dans toutes les cellulesTableView résultat youtube-répétition api

mon projet ViewController:

import UIKit 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet weak var tableview: UITableView! 

    var articles: [Article]? = [] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     fetchArticles() 
    } 

    func fetchArticles(){ 
     let urlRequest = URLRequest(url: URL(string: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=horses&type=video&maxResults=10&key=(apiKey)")!) 

     let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in 

      if error != nil { 
       print(error as Any) 
       return 
      } 

      self.articles = [Article]() 
      do { 
       let json = (try? JSONSerialization.jsonObject(with: data!, options: [])) as? [String : Any] 
       let article = Article() 
       if let articlesFromJson = json?["items"] as? [[String : Any]] { 
        for item in articlesFromJson { 


         if let snippet = item["snippet"] as? [String : Any],let title = snippet["title"]as? String,let desc = snippet["description"]as? String { 
          article.headline = title 
          article.desc = desc 
          self.articles?.append(article) 
         } 
        } 
        } 

       DispatchQueue.main.async { 
        self.tableview.reloadData() 

       }  
      }   
     } 
task.resume() 
    } 

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

     cell?.title.text = self.articles?[indexPath.row].headline! 
     cell?.desc.text = self.articles?[indexPath.row].desc! 

     return cell! 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.articles?.count ?? 0 
    } 

} 

Article.Swift:

import UIKit 

class Article: NSObject { 

    var headline: String? 
    var desc: String? 
} 

**ArticleCell :** 

import UIKit 

class ArticleCell: UITableViewCell { 


    @IBOutlet weak var title: UILabel! 
    @IBOutlet weak var desc: UILabel! 



    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 
    } 

} 

le problème est ----> le TableView Afficher le même titre et la distribution dans un cellules ll

+0

avez-vous vérifié le contenu du tableau? Sont-ils identiques ou différents? –

+0

ils sont différents:/ –

+0

dans la méthode cellforrow, imprimer (articles) et confirmer qu'il est identique ou non! –

Répondre

0

commentent juste cette ligne

DispatchQueue.main.async { 
       self.tableview.reloadData() 

      } 

/et l'ajouter après boucle
insert let article = Article() intérieur boucle
/

import UIKit 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    @IBOutlet weak var tableview: UITableView! 

    var articles: [Article]? = [] 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     fetchArticles() 
    } 

    func fetchArticles(){ 
     let urlRequest = URLRequest(url: URL(string: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=horses&type=video&maxResults=10&key=(apiKey)")!) 

     let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in 

      if error != nil { 
       print(error as Any) 
       return 
      } 

      self.articles = [Article]() 
      do { 
       let json = (try? JSONSerialization.jsonObject(with: data!, options: [])) as? [String : Any] 

       if let articlesFromJson = json?["items"] as? [[String : Any]] { 
        for item in articlesFromJson { 


         if let snippet = item["snippet"] as? [String : Any],let title = snippet["title"]as? String,let desc = snippet["description"]as? String { 
          let article = Article() 
          article.headline = title 
          article.desc = desc 
          self.articles?.append(article) 
         } 
        } 

        self.tableview.reloadData() 
        } 

       /*DispatchQueue.main.async { 
        self.tableview.reloadData() 

       } */ 

      }   
     } 
task.resume() 
    } 

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

     cell?.title.text = self.articles?[indexPath.row].headline! 
     cell?.desc.text = self.articles?[indexPath.row].desc! 

     return cell! 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.articles?.count ?? 0 
    } 

} 
+0

son même:/ –

+0

où est votre code cellulaire –

+0

func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellule = tableView.dequeueReusableCell (avecIdentifier: "articleCell", pour: indexPath) as? ArticleCell cellule? .title.text = self.articles? [IndexPath.row] .headline! cellule? .desc.text = self.articles? [IndexPath.row] .desc! cellule de retour! } –