簡體   English   中英

更改 collectionView 單元格下方的標題和描述 當集合視圖單元格更改時。 使用索引路徑執行此操作不起作用

[英]Changing the title and the description below the collectionView cell When the collection view cell change. Doing this with index path is not working

希望這張照片有助於描述案件 我正在使用我在更改集合視圖索引時獲得的索引路徑更新標題和描述,但這並沒有做正確的事情,因為當我滾動到結束並開始回滾與每個單元格顯示相關的描述和標題時那些意想不到的。

class IntroPage : BaseViewController {

@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var pageController: UIPageControl!
@IBOutlet weak var button: UIButton!
@IBOutlet weak var collectionView: UICollectionView!

private let reuseIdentifier = "CollectionViewCellIntroScene"

    let collectionViewImages : [UIImage] = [
#imageLiteral(resourceName: "maskGroup30"), 
#imageLiteral(resourceName: "maskGroup34"), 
#imageLiteral(resourceName: "maskGroup33")
]

let collectionViewTitleTexts : [String] = [
    "Track Your Fitness",
    "Win Exciting Deals",
    "Be a Challenge Winner!"
]

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.register(UINib(nibName: "CollectionViewCellIntroScene", bundle: nil), forCellWithReuseIdentifier: reuseIdentifier)
    renderCells()
}

func renderCells() {
    collectionView.isPagingEnabled = true
    collectionView.isScrollEnabled = true
    collectionView.showsVerticalScrollIndicator = false
    collectionView.showsHorizontalScrollIndicator = false
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    layout.sectionInset = UIEdgeInsets.zero
    collectionView.setCollectionViewLayout(layout, animated: true)
    collectionView.dataSource = self
    collectionView.delegate = self

}

}

集合視圖的委托

extension IntroPage : UICollectionViewDelegate {
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return collectionViewImages.count
}
}

集合視圖的數據源

extension IntroPage : UICollectionViewDataSource {

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    pageController?.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width)
}

func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {

    pageController?.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width)
}

在這里,當我向前滾動然后向后滾動時,標題和描述顯示錯誤的輸出

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCellIntroScene

    cell.image.image = collectionViewImages[indexPath.item]
    titleLabel.text = collectionViewTitleTexts[indexPath.item]
    
    return cell
}
}

您似乎正在使用通用的titleLabeldescriptionLabel並在cellForItemAt中設置標題和描述,這在加載單元格時被調用。 將以下內容從IntroPage移動到您的單元格CollectionViewCellIntroScene (通過添加標題和 desc 標簽來修改 xib):

@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!

cellForRowAt中:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCellIntroScene
    cell.image.image = collectionViewImages[indexPath.item]
    cell.titleLabel.text = collectionViewTitleTexts[indexPath.item]
    return cell
}

問題是您必須在滾動時清潔單元格為您的單元格創建單獨的 class 並使用那里的方法 prepareForReuse 來清潔您的單元格

override func prepareForReuse() {
    super.prepareForReuse()
    yourLabel.text = ""
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM