gpt4 book ai didi

ios - Swift willDisplayCell 和 PinterestLayout 不起作用

转载 作者:可可西里 更新时间:2023-11-01 00:55:56 24 4
gpt4 key购买 nike

我按照本教程像 pinterest 一样使用 collectionView。

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
}
}

最佳答案

我找到了解决方案。问题出在 PinterestLayout 教程上。我更改了代码,如下所示

在 PinterestLayout.swift

guard cache.isEmpty == true, let collectionView = collectionView else {
return
}

guard let collectionView = collectionView else {
return
}

它工作正常。

关于ios - Swift willDisplayCell 和 PinterestLayout 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48439148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com