0

私はスイフト3でプロジェクトに取り掛かり、UITableViewの中にUICollectionViewを含んでいます。したがって、私はUICollectionviewCellクラスにすべてのプロパティを割り当てることができましたが、サーバーから取得すると、プロジェクトを実行すると、UITableViewをスクロールしない限り、データはセルに表示されません。以下の3つのクラスの構造。ヘルプは大いに感謝します。UICollectionViewが正しくリロードされない

UIViewのコントローラー

extension ViewController : UITableViewDelegate, UITableViewDataSource { 

func numberOfSections(in tableView: UITableView) -> Int { 
    if tableView == self.allCategoriesTableView{ 
    return 4 
    } else { 
     return 1 
    } 
} 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if tableView == self.allCategoriesTableView { 
    return 1 
    }else if tableView == self.categoryTableView { 
    return categoryArray.count 
    }else{ 
    return menuBarArray.count 
    } 
} 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 

    if tableView == self.allCategoriesTableView { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "newCell", for: indexPath) as! CollectionItemsTableViewCell 
     if indexPath.section == 0 { 
      cell.fillCollectionView(with: category1) 
     }else if indexPath.section == 1 { 
      cell.fillCollectionView(with: category2) 
     }else if indexPath.section == 2 { 
      cell.fillCollectionView(with: category3) 
     } else { 
      cell.fillCollectionView(with: category3) 
     } 


    return cell 
    }else if tableView == self.categoryTableView { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryCell", for: indexPath) as! CategoryTableViewCell 
     cell.categoryNameLabel.text = categoryArray [indexPath.row] 
     cell.layer.cornerRadius = 8 
     cell.layer.masksToBounds = true 

     return cell 
    }else { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "MenuBarCell", for: indexPath) as! MenuBarTableViewCell 
     cell.menuBarContentLabel.text = menuBarArray [indexPath.row] 
     return cell 
    } 
} 

のUITableViewCellクラス

class CollectionItemsTableViewCell: UITableViewCell { 

@IBOutlet weak var collectionView: UICollectionView! 
var category1Json = [JSON?]() 

func fillCollectionView(with array: [JSON?]) { 
    self.category1Json = array 
    self.collectionView.reloadData() 
} 
override func awakeFromNib() { 
    super.awakeFromNib() 
    self.collectionView.delegate = self 
    self.collectionView.dataSource = self 
    loadCategoryDetails() 
    print("my array col is :",category1Json) 
    self.collectionView.reloadData() 

} 

extension CollectionItemsTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource { 


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    print("collection count is :", category1Json.count) 
    return category1Json.count 


} 

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

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

    if category1Json.count>0{ 

    cell.titleLabel.text = category1Json[indexPath.row]?["title"].stringValue 
    cell.healerNameLabel.text = category1Json[indexPath.row]?["healerName"].stringValue 
    cell.teaserDescriptionLabel.text = category1Json[indexPath.row]?["teaserDescription"].stringValue 
    cell.priceLabel.text = category1Json[indexPath.row]?["price"].stringValue 

    cell.musicImageView.image = imageArray [indexPath.row] 

    } 

    return cell 

} 
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 


    let clickedIndex = songArray[indexPath.row] 

    print(clickedIndex) 


} 
+0

なぜあなたはそのデータを使用していないので、あなたはapiコールをしていますか? –

+0

申し訳ありませんコードを編集しました – danutha

+0

'cellForItemAt'メソッドから' if condition'をチェックしてカウントを一度削除し、セルを再利用するためにラベルを直接設定してください。 –

答えて

0

あなたは、この次のコードを試してみてください。

dispatch_async(dispatch_get_main_queue()){
self.collectionView.reloadData()}

代わり

self.collectionView.reloadData()の

+0

私はCellforRowでこれを行うことになっていますか? – danutha

+0

ご迷惑をおかけして申し訳ございません。 –

+0

このコードを貼り付けてください。 – danutha

関連する問題