更改 collection 视图单元格下方的标题和说明 collection 视图单元格更改时。使用索引路径执行此操作不起作用
Changing the title and the description below the collectionView cell When the collection view cell change. Doing this with index path is not working
我正在使用我在 collection 视图索引时获得的索引路径更新标题和描述,但这并没有做正确的事情,因为当我滚动到结束并开始向后滚动与每个单元格关联的描述和标题显示意外的。
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
}
}
代表 Collection 视图
extension IntroPage : UICollectionViewDelegate {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return collectionViewImages.count
}
}
Collection 视图的数据源
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
(通过添加标题和描述标签修改 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
}
问题是您必须在滚动时清理单元格
为您的 Cell 创建单独的 class 并使用那里的方法 prepareForReuse 来清理您的 cell
override func prepareForReuse() {
super.prepareForReuse()
yourLabel.text = ""
}
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
}
}
代表 Collection 视图
extension IntroPage : UICollectionViewDelegate {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return collectionViewImages.count
}
}
Collection 视图的数据源
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
(通过添加标题和描述标签修改 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
}
问题是您必须在滚动时清理单元格 为您的 Cell 创建单独的 class 并使用那里的方法 prepareForReuse 来清理您的 cell
override func prepareForReuse() {
super.prepareForReuse()
yourLabel.text = ""
}