我想用一个看起来像应用程序商店的设计制作一个应用程序,所以我跟着让我们构建关于如何构建它的应用程序教程,结果非常好,当我在我的设备上测试它似乎当我快滚动集合 - 查看第一行中的数据发生在最后一行,当再次快速向上滚动时,应该在最后一行的数据我在第一行中找到它,当我向左和向右滚动时当单元格离开屏幕时,它会重新更新为正确的数据,但是当快速向上和向下快速滚动时,第一行和最后一行之间的数据会变得疯狂。 我在code.i结束时将图片添加到案例中。我花了5天时间试图解决这个问题但是没有运气我尝试了很多解决方案,比如将单元格中的数据重置为nil,然后再重新设置其他你会发现它在代码中但没有运气,我非常感谢您提供的任何帮助,谢谢
*更新 在@Joe Daniels回答之后,所有的数据都是主要的并且工作正常,除了快速滚动时它仍然会发疯的图像,但是在停止滚动到右图像的7/8秒后它会返回
包含垂直集合视图的第一个类 我使单元格的宽度等于视图的宽度,我尝试了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
}
**包含第一个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
答案 0 :(得分:0)
内部collectionView无法知道它已离开屏幕。在prepareForReuse中执行reload
答案 1 :(得分:0)
我找到了一个完美的解决方案,在使用了这个库(https://github.com/rs/SDWebImage)后,在使用xD的15天后对我有用了。
要在swift中使用它,请将此行添加到您的桥接标题
中#import <SDWebImage/UIImageView+WebCache.h>
在collectionViewCell中我使用了这一行
self.productImage.sd_setImage(with: myUrl , placeholderImage: UIImage(named:"yourPlaceHolderImage")