2016-09-20 17 views
0

我有一個問題,當我用segmentView集合視圖。集合視圖與段重複單元格

我有一個viewController具有segmentView具有4個元件(0 =>全部,1 =>照片,2 =>音像,3 =>視頻) 這裏的和例如:

enter image description here

我有一個的CollectionView顯示的數據取決於哪個類別從segmentController

0點擊

它的工作和顯示數據,這裏是我的的CollectionView方法來顯示數據

// MARK: Datasource collection method 

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

     if segmentStatues == 0 
     { 
      let cellForAll = collectionView.dequeueReusableCell(withReuseIdentifier: "AllItemsCell", for: indexPath) as! AllItemsCollectionViewCell 

      // reset elements before declare 

      cellForAll.cellImage.image = nil 
      cellForAll.playBtn.isHidden = true 

      cellForAll.layer.borderWidth = 1 
      cellForAll.layer.borderColor = UIColor(red:0.93, green:0.93, blue:0.93, alpha:1.0).cgColor 
      // When type is an image 
      if sociaPosts[(indexPath as NSIndexPath).row].postType == "image" 
      { 

       cellForAll.cellImage.sd_setImage(with: URL(string: sociaPosts[(indexPath as NSIndexPath).row].postUrl)) 


      }else if sociaPosts[(indexPath as NSIndexPath).row].postType == "audio" 
      { 

       cellForAll.cellImage.image = UIImage(named: "recorde_icon") 


      }else if sociaPosts[(indexPath as NSIndexPath).row].postType == "video" 
      { 
       cellForAll.cellImage.sd_setImage(with: URL(string: sociaPosts[(indexPath as NSIndexPath).row].thumbURL)) 
       cellForAll.playBtn.isHidden = false 
      } 

      return cellForAll 

     }else{ 

      // Cell if Images or videos or audios 

      let newCell = collectionView.dequeueReusableCell(withReuseIdentifier: "beepbeep", for: indexPath) as! userProfileImageCollectionViewCell 

      // If the type is images 

      if segmentStatues == 1 
      { 
       // Set image URL 

       newCell.cellImage.sd_setImage(with: URL(string: imagesPosts[(indexPath as NSIndexPath).row].postUrl)) 

       // Get image likes and comments and shares 

       newCell.likeCount.text = String(imagesPosts[(indexPath as NSIndexPath).row].likes_count) 
       newCell.commentCount.text = String(imagesPosts[(indexPath as NSIndexPath).row].comments_count) 
       newCell.shareCount.text = String(imagesPosts[(indexPath as NSIndexPath).row].shares_count) 

      } else if segmentStatues == 3 
      { 
       // For Video player 

      var player = newCell.customVid 
      player = Bundle.main.loadNibNamed("VieoPlayer", owner: self, options: nil)?.last as? videoPlayer 

      player?.newIntitVideoPlayer(forViewController: self, videoURL: videosPosts[indexPath.row].postUrl , videoThumbnail: URL(string: videosPosts[indexPath.row].thumbURL), onReady: nil) 
       // Set video URL and Thumbnail 

       // Get video likes and comments and shares 
       newCell.likeCount.text = String(videosPosts[(indexPath as NSIndexPath).row].likes_count) 
       newCell.commentCount.text = String(videosPosts[(indexPath as NSIndexPath).row].comments_count) 
       newCell.shareCount.text = String(videosPosts[(indexPath as NSIndexPath).row].shares_count) 

      }else if segmentStatues == 2 
      { 
       // Audio player 
       let ad = Bundle.main.loadNibNamed("AudioPlayerView", owner: self, options: nil)?.last as! AudioPlayerView 
       ad.frame = CGRect(x: (newCell.contentView.frame.size.width - newCell.contentView.frame.size.height * 0.6)/2, y: 20 , width: newCell.contentView.frame.size.height * 0.6, height: newCell.contentView.frame.size.height * 0.6) 
       ad.playImageView.image = nil 

       ad.tag = 6666 

       newCell.contentView.addSubview(ad) 

       // Set audio URL 
       ad.audioPath = URL(string: audiosPosts[indexPath.row].postUrl) 
       ad.viewInitialization() 

       // Get Audio likes and comments and shares 
       newCell.likeCount.text = String(audiosPosts[(indexPath as NSIndexPath).row].likes_count) 
       newCell.commentCount.text = String(audiosPosts[(indexPath as NSIndexPath).row].comments_count) 
       newCell.shareCount.text = String(audiosPosts[(indexPath as NSIndexPath).row].shares_count) 

      } 
      return newCell 
     } 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

     if self.segmentStatues == 0 
     { 

      return sociaPosts.count 
     }else if self.segmentStatues == 1 
     { 
      return imagesPosts.count 
     }else if self.segmentStatues == 2 
     { 
      return audiosPosts.count 
     }else 
     { 
      return videosPosts.count 
     } 
    } 

我在我的收藏視圖中的一個爲所有項目和其他2個定製單元(圖片 - 音頻 - 視頻)視圖

我的問題是數據的顯示,但是當我段之間的userProfileImageCollectionViewCell改變

複製細胞這2張圖片顯示的問題

顯示所有音頻單元,當我在音頻segmnet

enter image description here

單擊但經過我點擊圖像或視頻片段單元重複這樣

enter image description here

它在音頻發生 - 圖像 - 僅限視頻**(userProfileImageCollectionViewCell)**

和fin盟友這裏是我的userProfileImageCollectionViewCell代碼:

updateded:

import UIKit 
import SDWebImage 
class userProfileImageCollectionViewCell: UICollectionViewCell { 

    @IBOutlet weak var customVid: videoPlayer! 
    @IBOutlet weak var cellImage: UIImageView! 

    @IBOutlet weak var likeButton: UIButton! 

    @IBOutlet weak var likeCount: UILabel! 
    @IBOutlet weak var commentButton: UIButton! 

    @IBOutlet weak var commentCount: UILabel! 
    @IBOutlet weak var shareButton: UIButton! 
    @IBOutlet weak var shareCount: UILabel! 

    override func prepareForReuse() { 
     cellImage.sd_setImage(with: nil) 
     self.customVid.subviews.forEach({ $0.removeFromSuperview() }) 

    } 
} 
+0

在'prepareForReuse()'中的'userProfileImageCollectionViewCell'中,重置所有值(特別是可能出現在一個案例中而不出現在另一個案件中的值)。否則,你可以這樣做:'tableView:cellForRowAtIndexPath()',但由於你有一個自定義的單元格文件,我建議在'prepareForReuse()'中做。 – Larme

+0

segmentedController操作中的'collectionView.reloadData()'應該可以幫到你。 – Moriya

+0

最後一個條件只使用'else'而不是'else if segmentStatues == 2'。 –

回答

1

的問題是,UICollectionViewCell(以及UITableViewCell的方式)被重用。

簡單說就是: 當您滾動時單元格從屏幕上消失時,它可能會回到頂部/底部(根據滾動方向)。但它不是一個新的「乾淨」單元,它是以前的單元。這是重用系統。

所以,當你做了:newCell.contentView.addSubview(ad)你添加了每個新的子視圖,而不檢查是否有一個。子視圖在哪裏合理打樁。用3D Hierarchy Debug of XCode檢查它很容易。

既然你有一個userProfileImageCollectionViewCell文件,最好的解決方案是使用prepareForReuse()。 創建一個IBOutlet(我們稱之爲customVid),它將作爲其子視圖添加廣告/播放器。 在prepareForReuse()刪除customVid的所有子視圖。 在collectionView:cellForRowAtIndexPath:,做這樣的事情:

var player = Bundle.main.loadNibNamed("VieoPlayer", owner: self, options: nil)?.last as? videoPlayer 
player?.newIntitVideoPlayer(forViewController: self, videoURL: videosPosts 
newCell.customVid.addSubview(player) 

複製爲ad相同的機制(你可以使用相同的子視圖,它給你)。現在

,提出幾點建議不涉及您的問題:

名稱類開頭的大寫:userProfileImageCollectionViewCell =>UserProfileImageCollectionViewCell

我創建userProfileImageCollectionViewCell方法,使你的代碼更易讀。我不說話斯威夫特,所以我的代碼下一行可能無法編譯,但你應該得到的想法:

func fillWithVideo(videoParam video: CustomVideoClass) { 
    var player = newCell.customVid 
    player = Bundle.main.loadNibNamed("VieoPlayer", owner: self, options: nil)?.last as? videoPlayer 
    player?.newIntitVideoPlayer(forViewController: NOTself, videoURL: video.postUrl, videoThumbnail: URL(string: video.thumbURL), onReady: nil) 
    self.customVid.addSubview(player) 
    // Set video URL and Thumbnail 

    // Get video likes and comments and shares 
    self.likeCount.text = String(video.likes_count) 
    self.commentCount.text = String(video.comments_count) 
    self.shareCount.text = String(video.shares_count) 
} 

collectionView:cellForRowAtIndexPath:

//If it's the correct section 
let video = videosPosts[(indexPath as NSIndexPath).row] as CustomVideoClass 
cell fillWithVideo(video) 

注意,看來你使用UIViewController,因此您可能需要添加UIViewController參數以使newIntitVideoPlayer()方法正常工作,但我不想將它放在前一個示例中以使其更簡單。您可能需要更換線路:

cell fillWithVideo(video andViewController:self) 

func fillWithVideo(videoParam video: CustomVideoClass andViewController viewController: UIViewController) 
player?.newIntitVideoPlayer(forViewController: viewController, videoURL: video.postUrl, videoThumbnail: URL(string: video.thumbURL), onReady: nil) 

等等。對於每個部分特定部分。它讓你更容易閱讀你在做什麼。只處理collectionView:cellForRowAtIndexPath:的邏輯(哪一部分應該得到哪個項目等等),其餘部分是可以適應的單元格。

+0

非常感謝您的回答和您的解釋:)) – Muhammed

相關問題