我有一個問題,當我用segmentView集合視圖。集合視圖與段重複單元格
我有一個viewController
具有segmentView具有4個元件(0 =>全部,1 =>照片,2 =>音像,3 =>視頻) 這裏的和例如:
我有一個的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
單擊但經過我點擊圖像或視頻片段單元重複這樣
它在音頻發生 - 圖像 - 僅限視頻**(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() })
}
}
在'prepareForReuse()'中的'userProfileImageCollectionViewCell'中,重置所有值(特別是可能出現在一個案例中而不出現在另一個案件中的值)。否則,你可以這樣做:'tableView:cellForRowAtIndexPath()',但由於你有一個自定義的單元格文件,我建議在'prepareForReuse()'中做。 – Larme
segmentedController操作中的'collectionView.reloadData()'應該可以幫到你。 – Moriya
最後一個條件只使用'else'而不是'else if segmentStatues == 2'。 –