2017-06-20 4 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() 

       } 
      }) 
     } 
    } 

アセットテーブルのサーバーデータストアからデータを取得します。

そしてassetTableデータがCollectionViewCellに表示されます。

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は関連するデータを表示しませんでした。 HomeVideoCellサブクラスで

+0

編集中のデータをクリーンアップし、いくつかのより多くのstuffsを追加することで、シナリオを理解することが、より明確にします。 –

+0

したがって、基本的には、tableViewCellでコレクションビューを使用しようとしています。 問題は、テーブルビューのセルをデキューすることに関連しています。 各tableViewCellのコレクションビューのデータを個別に設定し、表示が必要なtableViewのcellForRowAtIndex内のデータを渡す必要があります。 各セルには、コレクションビューデータを所有する/ store /が含まれます。 –

+0

tableViewCellのクラスにディクショナリを作成して配列し、webserviceから取得したtableViewのcellForRowAtIndexPathに尊重したデータを渡します。 –

答えて

0

は、あなたの質問prepareforreuse

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

HomeVideoCell.swiftでセルを呼び出せません。電話をかけるには? @MeghsDhameliya –

+0

あなたが既にセルに入っているのと同じように自己を使用してください –

+0

@SanSanあなたの問題が解決したら@SanSanとして解決してください –

関連する問題