2017-06-20 45 views
1

我從TableViewCell中獲得了json鏈接數據,然後從服務器檢索該數據,並在具有相關TableViewCell數據的collectionView中顯示。 如何在swift3中顯示這些數據?請幫幫我。如何使用swift3在TableViewCell的CollectionViewCell中從服務器動態顯示數據?

我從TableViewCell中獲取了url鏈接(mainThemeList.main_associated_url,main_name)。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 



     let mainThemeList = mainHomeThemeTable[(indexPath as NSIndexPath).row] 

     let cell = tableView.dequeueReusableCell(withIdentifier: "homecell") as! HomeCategoryRowCell 



     DispatchQueue.main.async { 
      cell.categoryTitle.text = mainThemeList.main_name 
      cell.mainAssociatedURL.text = mainThemeList.main_associated_url 


      self.prefs.set(mainThemeList.main_name, forKey: "main_name") 
      cell.categoryTitle.font = UIFont.boldSystemFont(ofSize: 17.0) 
      cell.collectionView.reloadData() 

     } 

     DispatchQueue.main.async { 

      self.retrieveDataFromServer(associated_url: mainThemeList.main_associated_url,main_name: mainThemeList.main_name) 
     } 

     return cell 
    } 

然後再從服務器獲取與數據相關的url鏈接數據。

private func retrieveDataFromServe(associated_url : String , main_name: String) { 
     SwiftLoading().showLoading() 
     if Reachability().isInternetAvailable() == true { 

      self.rest.auth(auth: prefs.value(forKey: "access_token") as! String!) 
      rest.get(url: StringResource().mainURL + associated_url , parma: [ "show_min": "true" ], finished: {(result : NSDictionary, status : Int) -> Void in 


       self.assetsTable.removeAll() 
       if(status == 200){ 
        let data = result["data"] as! NSArray 
        if (data.count>0){ 
         for item in 0...(data.count) - 1 { 

          let themes : AnyObject = data[item] as AnyObject 
          let created = themes["created"] as! String 
          let assets_id = themes["id"] as! Int 
          let name = themes["name"] as! String 
          var poster_img_url = themes["poster_image_url"] as! String 
          let provider_id = themes["provider_id"] as! Int 

          poster_img_url = StringResource().posterURL + poster_img_url 

          self.assetsTable.append(AssetsTableItem(main_name: main_name,created: created,assets_id: assets_id, name: name, poster_image_url: poster_img_url,provider_id: provider_id)) 

         } 
        } 

        SwiftLoading().hideLoading() 

       }else{ 

        SwiftLoading().hideLoading() 

       } 
      }) 
     } 
    } 

從assetsTable中的服務器數據存儲檢索數據。

然後在CollectionViewCell中顯示assetsTable數據。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 



    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! HomeVideoCell 

      cell.movieTitle.text = list.name 
      cell.imageView.image = list.image 



    return cell 

} 

我的問題是collectionViewCell數據與以前assetsTable數據重複,並沒有在CollectionView中顯示正確的數據。

enter image description here

我tableViewCell表演(動作,劇情)標籤和我的CollectionViewCell電影放映名稱和動畫影像。我從服務器檢索CollectionViewCell的數據,但CollectionViewCell沒有顯示相關數據。

+0

編輯你的問題,使之更加明確通過添加一些東西瞭解的情況。 –

+0

所以基本上你正試圖在tableViewCell中使用集合視圖。 並且您的問題與表視圖單元出列有關。 你必須爲每個tableViewCell的集合視圖設置不同的數據,將數據傳遞給需要顯示的tableView的cellForRowAtIndex。 每個單元格將包含/存儲/擁有它自己的集合視圖數據。 –

+0

在tableViewCell的類中創建和數組字典,並傳遞從webservice獲取的tableView的cellForRowAtIndexPath中的受尊敬數據。 –

回答

0

在HomeVideoCell子類清理數據prepareforreuse

override func prepareForReuse() { 
    super.prepareForReuse() 
    self.movieTitle.text = "" 
    self.imageView.image = nil 
} 
+0

我無法在HomeVideoCell.swift中調用單元格。如何打電話? @MeghsDhameliya –

+0

使用自己,因爲你已經在單元格 –

+0

@SanSan請標記爲已解決如果您的問題修復 –

相關問題