![](/img/trans.png)
[英]Inserting collectionview inside cell of another collection view
[英]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
}
}
您似乎正在使用通用的titleLabel
和descriptionLabel
並在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.