2016-11-03 58 views
-1

我想知道如果我在表視圖控制器中有幾篇文章,它們是從某些API加載和分析的,應該如何表現。然後我想點擊一些文章來查看他的細節。完成segue後再次加載數據

我應該加載上的所有數據啓動所有的文章,然後通過具體的整篇文章接下來詳細信息表控制器或者只發送文章ID,並在新表控制器負載再次全一文與傳入ID?

我認爲這是更好的第二種方法,但不知道該怎麼做。任何幫助?非常感謝。

編輯:添加的代碼

MainTableViewController:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    let articleId = articles[indexPath.row].Id 
    let destination = DetailTableViewController() 
    destination.articleId = articleId 

    destination.performSegue(withIdentifier: "Main", sender: self) 
} 

DetailTableViewController:

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.rowHeight = UITableViewAutomaticDimension 
    tableView.estimatedRowHeight = 140 

    // Vyzkoušíme jestli jsme připojeni do internetu 
    if Helper.hasConnectivity() { 
     // Zapneme načítací obrazovku 
     setLoadingScreen() 

     // Načteme jídla 
     loadMainArticles() 
    } else { 
     promptAlert(title: "Upozornění", message: "Ujistěte se, že Vaše zařízení je připojené k internetu") 
    } 
} 

private func loadMainArticles() { 
    ApiService.sharedInstance.fetchArticle(part: "GetArticle", articleId: "brananske-posviceni--spravna-lidova-zabava-s-pecenou-husou") { (articles: [Article]) in 
     self.removeLoadingScreen() 

     self.tableView.separatorStyle = .singleLine 

     if articles.count == 0 { 
      self.promptAlert(title: "Upozornění", message: "Žádná data") 
     } 

     self.selectedArticle = articles[0] 
     self.tableView.reloadData() 
    } 
} 

ApiService:

func fetchArticle(part: String, articleId: String, completion: @escaping ([Article]) ->()) { 
    var Id = "" 
    var Title = "" 
    var Content = "" 
    var Picture = "" 
    var PublishDate = "" 

    let url = NSURL(string: "http://xxx") 

    URLSession.shared.dataTask(with: url! as URL) { (data, response, error) in 

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

     do { 
      let json = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) 

      var articles = [Article]() 

      for dictionary in json as! [[String: AnyObject]] { 
       . 
       . 
       . 

       let article = Article(Id: Id, Title: Title, Content: Content, Picture: Picture, PublishDate: PublishDate, Categories: Categories) 

       articles.append(article!) 
      } 

      DispatchQueue.main.async(execute: { 
       completion(articles) 
      }) 

     } 

     }.resume() 

} 

的問題是,當我點擊行後再執行segue,然後loadMainArticles。該方法被觸發,但始終在URLSession行上停止,並立即跳轉到resume(),並且永遠不會觸發完成塊。有什麼理由?

回答

0

這是根據文章數據模型大小應該做的選擇。

您應該注意您需要在TableView單元格上顯示哪些數據以及文章詳細信息頁上的哪些數據。

我想你,以顯示:

  • 標題和詳情頁

貼小時TableView中

  • 標題,小時,長的描述,也許有些像在這種情況下,我會reccommend您只下載每個文章項目的標題和小時,然後在詳細頁面中下載單個項目詳細信息(避免下載未使用的內容,因爲用戶無法打開每個項目)。

    否則,如果數據量是相似的,下載在第一次連接並打開詳細信息頁面所有信息不作任何其他網絡請求。

    我認爲這是更好的第二種方法,但不知道該怎麼做。

    您需要從TableView選定的單元格中檢索文章ID並使用prepareForSegue將其傳遞到DetailPageViewController。在DetailPageViewController中,ViewDidLoad將文章ID發送到您的後端api並檢索文章細節以顯示。

  • +0

    我已經完成了這個,就像您在加載所有文章時所說的那樣。在viewDidLoad中我稱之爲API方法GetAllArticles(使用URLSession.shared.datatask),並隨時返回完成塊我想要的東西,但是當我在PageDetailTableViewController賽格瑞,我通過賽格瑞通過文章ID後運行這一點,那麼方法總是在第一行停止( URLSession.shared.datatask),然後跳轉到.resume()因此,沒有完成塊被觸發...這是我面臨的問題... –

    +0

    您需要在您的問題中添加一些代碼.. – lubilis

    +0

    我編輯了問題,添加了一些代碼... –

    相關問題