在快速滚动 collectionview 时,第一行数据在最后一行重复
While fast scrolling collectionview the first row data duplicate in the last row
我想制作一个设计看起来像应用程序商店的应用程序,所以我按照让我们构建该应用程序教程来构建它,结果非常好,当我在我的设备上测试它时,似乎当我快速滚动集合-查看第一行中的数据发生在最后一行,当再次快速向上滚动时,应该在最后一行的数据我在第一行找到它,当我向左滚动时,就在行中,当单元格离开屏幕时,它会重新更新为正确的数据,但是当快速上下滚动时,第一行和最后一行之间的数据会变得疯狂。
我在 code.i 的末尾添加了图像,花了 5 天时间试图解决这个问题,但一点运气都没有 我尝试了很多解决方案,比如在重置单元格之前将单元格中的数据重置为 nil在代码中找到它但没有运气,非常感谢您提供的任何帮助,谢谢
*更新
在@Joe Daniels 回答之后,所有数据都是主要的并且工作正常,除了图像它在快速滚动时仍然变得疯狂但是它 return 在停止滚动到正确的图像 7/8 秒后
第一个 class 包含垂直集合视图
我使单元格的宽度等于视图的宽度,我尝试了 cell.tag == index-path.row 解决方案,但它没有用
class HomePageVC: UIViewController , UICollectionViewDataSource , UICollectionViewDelegate , UICollectionViewDelegateFlowLayout ,SWRevealViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
mainProductsRow.delegate = self
mainProductsRow.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let count = productCategory?.count {
return count + 1
}
return 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.row == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: LargeHomeCategoriesCell.identifier, for: indexPath) as! LargeHomeCategoriesCell
cell.categoriesHomePageVC = self
cell.catIndexPath = indexPath.row
cell.productCategories = productCategory
cell.seeMore.addTarget(self, action: #selector(self.seeMoreCat(_:)), for: UIControlEvents.touchUpInside)
return cell
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RowCell", for: indexPath) as! HomeCategoriesCell
cell.categoriesHomePageVC = self
cell.catIndexPath = indexPath.row
cell.seeMore.tag = (indexPath.row - 1 )
cell.seeMore.addTarget(self, action: #selector(self.seeMore(_:)), for: UIControlEvents.touchUpInside)
// cell.tag = indexPath.row - 1
cell.productCategory = nil
//if cell.tag == indexPath.row - 1{
cell.productCategory = productCategory?[indexPath.row - 1 ]
// }
return cell
}
**第二个 Class 包含第一个 collectionview 单元格中的水平 collectionview **
在 cell-for-item-At-index-Path 中,我在视图加载时打印了 index-path.row 0 , 1 ,2 并且没有打印最后一个索引 '3' 和同样的东西当我滚动到集合视图的末尾时再次发生
class HomeCategoriesCell: UICollectionViewCell , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout , UICollectionViewDelegate{
@IBOutlet weak var seeMore: UIButton!
@IBOutlet weak var categorytitle: UILabel!
@IBOutlet weak var productsCollectionView: UICollectionView!
let favFuncsClass = FavItemsFunctionality()
let onCartFuncsClass = OnCartFunctionality()
var productCategory : ProductCategories? {
didSet {
if let categoryTitle = productCategory?.name {
categorytitle.text = categoryTitle
}
}
override func awakeFromNib() {
recivedNotification()
productsCollectionView.delegate = self
productsCollectionView.dataSource = self
productsCollectionView.backgroundColor = UIColor.clear
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let count = productCategory?.products?.count {
return count
}
return 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print(indexPath.row)
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HProductCell", for: indexPath) as! HomeProductCell
cell.tag = indexPath.row
cell.productImage.image = #imageLiteral(resourceName: "PlaceHolder")
cell.productTitle.text = nil
cell.productPrice.text = nil
cell.discountLabel.text = nil
cell.preDiscountedPrice.text = nil
cell.favButton.setImage(UIImage(named:"Heart_icon"), for: UIControlState.normal)
cell.addToCart.setImage(UIImage(named:"cart"), for: UIControlState.normal)
cell.configCell(products: nil)
if cell.tag == indexPath.row {
cell.configCell(products: productCategory?.products?[indexPath.item])
}
cell.catNum = indexPath.row
return cell
}
// Thanks to Joe Daniels i added this code and my nightmare become way less scary :P
override func prepareForReuse() {
super.prepareForReuse()
productsCollectionView.reloadData()
}
}
产品单元格
我尝试了 prepare-For-Reuse() 并将所有数据重置为 nil 但仍然没有用
class HomeProductCell: UICollectionViewCell {
func configCell(products :productDetails?){
if let title = products?.name {
self.productTitle.text = title
}else { self.productTitle.text = nil}
if let price = products?.price {
self.productPrice.text = "\(price)"
}else { self.productPrice.text = nil }
}
override func prepareForReuse() {
super.prepareForReuse()
self.productImage.image = #imageLiteral(resourceName: "PlaceHolder")
productTitle.text = nil
productPrice.text = nil
discountLabel.text = nil
preDiscountedPrice.text = nil
favButton.setImage(UIImage(named:"Heart_icon"), for: UIControlState.normal)
addToCart.setImage(UIImage(named:"cart"), for: UIControlState.normal)
}
我截屏了你可以找到它 here
内部 collectionView 无法知道它离开了屏幕。在 prepareForReuse
中做一个 reload
经过 15 天的痛苦 xD 我找到了对我来说神奇的完美解决方案我使用了这个库 (https://github.com/rs/SDWebImage)。
要在 swift 中使用它,请将此行放在您的桥接头中
#import <SDWebImage/UIImageView+WebCache.h>
并且在 collectionViewCell 中我使用了这一行
self.productImage.sd_setImage(with: myUrl , placeholderImage: UIImage(named:"yourPlaceHolderImage")
我想制作一个设计看起来像应用程序商店的应用程序,所以我按照让我们构建该应用程序教程来构建它,结果非常好,当我在我的设备上测试它时,似乎当我快速滚动集合-查看第一行中的数据发生在最后一行,当再次快速向上滚动时,应该在最后一行的数据我在第一行找到它,当我向左滚动时,就在行中,当单元格离开屏幕时,它会重新更新为正确的数据,但是当快速上下滚动时,第一行和最后一行之间的数据会变得疯狂。 我在 code.i 的末尾添加了图像,花了 5 天时间试图解决这个问题,但一点运气都没有 我尝试了很多解决方案,比如在重置单元格之前将单元格中的数据重置为 nil在代码中找到它但没有运气,非常感谢您提供的任何帮助,谢谢
*更新 在@Joe Daniels 回答之后,所有数据都是主要的并且工作正常,除了图像它在快速滚动时仍然变得疯狂但是它 return 在停止滚动到正确的图像 7/8 秒后
第一个 class 包含垂直集合视图 我使单元格的宽度等于视图的宽度,我尝试了 cell.tag == index-path.row 解决方案,但它没有用
class HomePageVC: UIViewController , UICollectionViewDataSource , UICollectionViewDelegate , UICollectionViewDelegateFlowLayout ,SWRevealViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
mainProductsRow.delegate = self
mainProductsRow.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let count = productCategory?.count {
return count + 1
}
return 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.row == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: LargeHomeCategoriesCell.identifier, for: indexPath) as! LargeHomeCategoriesCell
cell.categoriesHomePageVC = self
cell.catIndexPath = indexPath.row
cell.productCategories = productCategory
cell.seeMore.addTarget(self, action: #selector(self.seeMoreCat(_:)), for: UIControlEvents.touchUpInside)
return cell
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RowCell", for: indexPath) as! HomeCategoriesCell
cell.categoriesHomePageVC = self
cell.catIndexPath = indexPath.row
cell.seeMore.tag = (indexPath.row - 1 )
cell.seeMore.addTarget(self, action: #selector(self.seeMore(_:)), for: UIControlEvents.touchUpInside)
// cell.tag = indexPath.row - 1
cell.productCategory = nil
//if cell.tag == indexPath.row - 1{
cell.productCategory = productCategory?[indexPath.row - 1 ]
// }
return cell
}
**第二个 Class 包含第一个 collectionview 单元格中的水平 collectionview ** 在 cell-for-item-At-index-Path 中,我在视图加载时打印了 index-path.row 0 , 1 ,2 并且没有打印最后一个索引 '3' 和同样的东西当我滚动到集合视图的末尾时再次发生
class HomeCategoriesCell: UICollectionViewCell , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout , UICollectionViewDelegate{
@IBOutlet weak var seeMore: UIButton!
@IBOutlet weak var categorytitle: UILabel!
@IBOutlet weak var productsCollectionView: UICollectionView!
let favFuncsClass = FavItemsFunctionality()
let onCartFuncsClass = OnCartFunctionality()
var productCategory : ProductCategories? {
didSet {
if let categoryTitle = productCategory?.name {
categorytitle.text = categoryTitle
}
}
override func awakeFromNib() {
recivedNotification()
productsCollectionView.delegate = self
productsCollectionView.dataSource = self
productsCollectionView.backgroundColor = UIColor.clear
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let count = productCategory?.products?.count {
return count
}
return 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print(indexPath.row)
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HProductCell", for: indexPath) as! HomeProductCell
cell.tag = indexPath.row
cell.productImage.image = #imageLiteral(resourceName: "PlaceHolder")
cell.productTitle.text = nil
cell.productPrice.text = nil
cell.discountLabel.text = nil
cell.preDiscountedPrice.text = nil
cell.favButton.setImage(UIImage(named:"Heart_icon"), for: UIControlState.normal)
cell.addToCart.setImage(UIImage(named:"cart"), for: UIControlState.normal)
cell.configCell(products: nil)
if cell.tag == indexPath.row {
cell.configCell(products: productCategory?.products?[indexPath.item])
}
cell.catNum = indexPath.row
return cell
}
// Thanks to Joe Daniels i added this code and my nightmare become way less scary :P
override func prepareForReuse() {
super.prepareForReuse()
productsCollectionView.reloadData()
}
}
产品单元格 我尝试了 prepare-For-Reuse() 并将所有数据重置为 nil 但仍然没有用
class HomeProductCell: UICollectionViewCell {
func configCell(products :productDetails?){
if let title = products?.name {
self.productTitle.text = title
}else { self.productTitle.text = nil}
if let price = products?.price {
self.productPrice.text = "\(price)"
}else { self.productPrice.text = nil }
}
override func prepareForReuse() {
super.prepareForReuse()
self.productImage.image = #imageLiteral(resourceName: "PlaceHolder")
productTitle.text = nil
productPrice.text = nil
discountLabel.text = nil
preDiscountedPrice.text = nil
favButton.setImage(UIImage(named:"Heart_icon"), for: UIControlState.normal)
addToCart.setImage(UIImage(named:"cart"), for: UIControlState.normal)
}
我截屏了你可以找到它 here
内部 collectionView 无法知道它离开了屏幕。在 prepareForReuse
中做一个reload
经过 15 天的痛苦 xD 我找到了对我来说神奇的完美解决方案我使用了这个库 (https://github.com/rs/SDWebImage)。
要在 swift 中使用它,请将此行放在您的桥接头中
#import <SDWebImage/UIImageView+WebCache.h>
并且在 collectionViewCell 中我使用了这一行
self.productImage.sd_setImage(with: myUrl , placeholderImage: UIImage(named:"yourPlaceHolderImage")