我想用一個看起來像應用商店的設計製作應用程序,所以我跟着讓我們構建應用程序教程,瞭解如何構建它,結果非常好,當我在我的設備上測試它時,似乎當我快速滾動收藏查看第一行中的數據發生在最後一行並且再次快速滾動時,應該在最後一行中找到的數據它在第一行,當我在該行左右滾動時,單元格離開屏幕時,它重新更新到正確的數據,但當快速再次上下滾動時,第一行和最後一行之間的數據變得瘋狂。 我在code.i花了5天的情況下,試圖解決這個問題,但沒有運氣,我嘗試了很多的解決方案,如重置它的細胞中的數據爲零之前,重置它和其他許多你會發現它的代碼,但沒有運氣,我真的很感激任何幫助,您可以提供,感謝快速滾動collectionview查看最後一行的第一行數據dublicate
*更新 後@Joe丹尼爾斯回答所有的數據是主食和除圖像做工精細仍然發瘋時快速滾動但它停止後7/8秒停止滾動到右側圖像
第一類包含垂直collectionview 我提出的單元的寬度等於視圖的寬度,我試過cell.tag ==索引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是在第一個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()
}
}
產物的細胞 我嘗試了準備,對於重用()和reseted所有數據爲零,但仍然沒有工作
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
我想它和它的工作對字符串數據,如魔術,而是當它快速滾動它的圖像錯過和dublicate,並顯示在錯誤的單元格,但它約8/9秒後改變爲正確的形象,但毫無疑問,你救了我非常感謝你^ - ^。你有什麼想法如何防止圖像做這個毛刺? – killvak