0

我有問題,來更新我的UICollectionView。我有以下設置,其中包含嵌套collectionViews。更新嵌套UICollectionView - 斯威夫特3

enter image description here

的homeControllerView與的CollectionView 1 collectionViewController,啓用尋呼。 CollectionView 1的每個單元格都是一個自己的集合視圖,例如的CollectionView A,B,...這樣做的原因是,每一個的CollectionView A,B,...是一個進料,其提取,然後將其顯示爲單元格A1,A2,B1 ...信息

當HomeViewController被加載所有的CollectionViews A,B ...被填充,每個(Main)Cell 1,2,將開始獲取信息。一些Feed正在提取個人信息,即綁定到當前登錄的用戶。

切換到另一個用戶後,我想要更新(Main)collectionView,以便所有Cell 1 ,2,...正在盯着再次獲取信息。所以在我的LoginController中,我打電話

self.homeController?.collectionView?.reloadData() 

但是,這似乎只適用於當前可見的單元格,顯示示例中的單元格2。此外重裝項目只對那些有顯着的成果:

self.homeController?.collectionView?.reloadItems(at: (self.homeController?.collectionView?.indexPathsForVisibleItems)!) 

在我homeViewController我重寫cellForItem功能這樣

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    switch indexPath.item { 
     case 0: 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: incomingFeedCellId, for: indexPath) as! IncomingFeedCell 
      return cell 
     case 1: 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: popularFeedCellId, for: indexPath) as! PopularFeedCell 
      return cell 
     case 2: 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: favoritFeedCellId, for: indexPath) as! FavoritFeedCell 
      return cell 
     case 3: 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: personalFeedCellId, for: indexPath) as! PersonalFeedCell 
      return cell 
     default: 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: incomingFeedCellId, for: indexPath) as! IncomingFeedCell 
      return cell 
    } 
} 

但隨着.reloadData()的cellForItem功能不會再次調用,所以一旦啓動,單元格不會再更新。

每個FeedCell具有被稱爲至少一次細胞創建取()函數:

func fetch(){ 
    posts = [Post]() 
    let ref = FIRDatabase.database().reference() 

    ref.child("posts_incoming").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in 
     if let dictionary = snapshot.value as? NSDictionary { 
      let post = Post() 
      post.imageUrl = dictionary["image_url"] as? String 
      post.caption = dictionary["caption"] as? String 
      ... 
      ... 
      self.posts?.append(post) 

      DispatchQueue.main.async(execute: { 
       self.collectionView.reloadData() 
      }) 
     } 
    }, withCancel: nil) 
} 

目前我跟蹤(主)內的用戶id的細胞,從而,當該單元被出隊(例如滑動到下一個進料)它會檢查,如果UID已經改變,如果是這樣,取將再次執行...但我敢肯定,一定是對這個問題的一個更好的解決方案。有沒有辦法,放棄所有細胞,並強制每個細胞的新的初始化?

回答

0

我建議創建一個自定義類的UICollectionView,所有的飼料提取調用應該在定製UICollectionView類進行。 此外,爲了保持簡單,就像不嵌套UICollectionViews,您可以使用:

主要UICollectionView用於分頁並在UICollectionView的單元格內添加UITableview。

所以重裝會是這樣:

UICollectionView在UIViewController中

的UITableView在UICollectionView自定義類

而且必須使用dequeueReusableCell始終否則太多的內存將被分配使您應用崩潰。

+0

感謝您的回答,但我不明白這是怎麼幫助。如果我將創建CollectionView 1作爲一個自定義的CollectionView和一個新的函數來更新單元格,我仍然無法訪問那些不可見的單元格,對嗎? –

+0

是啊看看你是否已經修復了5個超級單元格,你可以做一件事,用戶ScrollView的內容大小是視圖的5倍。然後在每個containerView中添加5個包含分離UIViewControllers的容器視圖。 這樣你可以輕鬆管理一切。 – ankit