我按照本教程使用collectionView就像pinterest一样。
https://www.raywenderlich.com/164608/uicollectionview-custom-layout-tutorial-pinterest-2
[已更新]
' willDisplay cell'方法对我不起作用。
当displaycell方法唤醒但是collectionView没有重新加载时,我得到了新的数据。
我把所有代码都放了。请帮帮我!
import UIKit
import Kingfisher
class CategoryPageVC: UIViewController , UICollectionViewDelegate , UICollectionViewDataSource {
var mainID:Int!
var categoryData = [CategoryPageData]()
var descriptionText: String!
@IBOutlet var descriptionLabel: UILabel!
var phoneLanguageUser:String!
@IBOutlet var loadingView: UIView!
@IBOutlet var collectionView: UICollectionView!
@IBOutlet var emptyView: UIView!
@IBOutlet var gifImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
print(self.mainID!)
loaderImages = createImageArray(total: 89, imagePrefix: "Loader")
animate(imageView: gifImageView, images: loaderImages)
phoneLanguageUser = NSLocale.current.languageCode
if phoneLanguageUser != "tr" && phoneLanguageUser != "en" {
phoneLanguageUser = "en"
}
self.loadingView.isHidden = false
self.loadingView.backgroundColor = UIColor.white
self.emptyView.isHidden = true
self.collectionView.delegate = self
self.collectionView.dataSource = self
if let layout = self.collectionView?.collectionViewLayout as? PinterestLayout {
layout.delegate = self
}
// --------------------------------- JSON DATA ----------------------------------------
let url = URL(string: "....")
let task = URLSession.shared.dataTask(with:url!, completionHandler: {(data, response, error) in
....
....
....
})
task.resume()
// --------------------------------- JSON DATA END ----------------------------------------
}
override func viewDidAppear(_ animated: Bool) {
let when = DispatchTime.now() + 5 // change 2 to desired number of seconds
DispatchQueue.main.asyncAfter(deadline: when) {
if self.categoryData.count == 0 {
self.loadingView.isHidden = true
self.emptyView.isHidden = false
}
}
self.collectionView.reloadData()
print(categoryData.count)
}
// --------------------------- COLLECTION VIEW DATA ----------------------------------
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return categoryData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PPColCell", for: indexPath as IndexPath) as! PPCollectionCell
cell.imageView.image = self.categoryData[indexPath.row].image
return cell
}
// --------------------------- COLLECTION VIEW DATA END --------------------------------
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let itemSize = (collectionView.frame.width - (collectionView.contentInset.left + collectionView.contentInset.right + 10)) / 2
return CGSize(width: itemSize, height: itemSize)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "categoryToProductSegue", sender: self.categoryData[indexPath.row].uID)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "categoryToProductSegue" {
if let destination = segue.destination as? ProductPageVC {
if let UID = sender as? String {
destination.UID = UID
}
}
}
}
var loaderImages: [UIImage] = []
func createImageArray(total: Int, imagePrefix:String) -> [UIImage] {
var imageArray : [UIImage] = []
for imageCount in 1..<total {
let imageName = "\(imagePrefix)-\(imageCount).png"
let image = UIImage(named: imageName)!
imageArray.append(image)
}
return imageArray
}
func animate(imageView: UIImageView, images: [UIImage]) {
imageView.animationImages = images
imageView.animationDuration = 2
imageView.animationRepeatCount = 0
imageView.startAnimating()
}
@objc func loadMoreData(){
let url = URL(string: ...)
....
...
....
})
task.resume()
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
let lastElement = categoryData.count - 2
if indexPath.row == lastElement {
loadMoreData()
}
}
}
extension CategoryPageVC: UICollectionViewDelegateFlowLayout {
}
extension CategoryPageVC: PinterestLayoutDelegate {
func collectionView(_ collectionView: UICollectionView,
heightForPhotoAtIndexPath indexPath:IndexPath) -> CGFloat {
let oran = self.categoryData[indexPath.item].image.size.width / self.collectionView.frame.size.width * 2
return self.categoryData[indexPath.item].image.size.height / oran
}
}
答案 0 :(得分:3)
我找到了解决方案。问题出在PinterestLayout教程上。我更改了代码,如下面的
在PinterestLayout.swift
guard cache.isEmpty == true, let collectionView = collectionView else {
return
}
到
guard let collectionView = collectionView else {
return
}
工作正常。