[英]How to load non static images from an array into collection view in Swift
I am trying to get images URL'S from my firebase firestore and save them in an array.我正在尝试从我的 firebase firestore 中获取图像 URL 并将它们保存在一个数组中。
then I would like to set up a collection view that would display the images in the array in a sliding manner similar to Instagram (horizontally).然后我想设置一个集合视图,以类似于 Instagram(水平)的滑动方式显示数组中的图像。
I am retrieving the values of my images url's and saving them in variables and then setting them in an array, but when I set up my collection view it does not work, I believe that the collection view functions receive the array with empty values since I am retrieving and storing them in the view did load method我正在检索我的图像 url 的值并将它们保存在变量中,然后将它们设置在一个数组中,但是当我设置我的集合视图时它不起作用,我相信集合视图函数接收带有空值的数组,因为我正在检索并将它们存储在视图中确实加载了方法
what am I doing wrong?我究竟做错了什么?
any help on this?有什么帮助吗? thank you.
谢谢你。 here is my code:
这是我的代码:
public var imgArray = [URL]()
var getHeaderURL : URL!
var getHeaderURL2 : URL!
var getHeaderURL3: URL!
var getHeaderURL4: URL!
var headerImgURL:String!
var headerImgURL2:String!
var headerImgURL3:String!
var headerImgURL4:String!
override func viewDidLoad(){
db.collection("Shops").document(getKey!)
.addSnapshotListener { [self] snapshot, error in
if error != nil {
print(error ?? "Couldn't update text field TextUser according to database")
}
else
{
//getting url's as strings
self.headerImgURL = snapshot?["ShopHeaderImg"] as? String
self.headerImgURL2 = snapshot?["ShopHeaderImg2"] as? String
self.headerImgURL3 = snapshot?["ShopHeaderImg3"] as? String
self.headerImgURL4 = snapshot?["ShopHeaderImg4"] as? String
}
//converting url strings into url type.
getHeaderURL = URL(string: self.headerImgURL)
getHeaderURL2 = URL(string: self.headerImgURL2)
getHeaderURL3 = URL(string: self.headerImgURL3)
getHeaderURL4 = URL(string: self.headerImgURL4)
self.imgArray = [getHeaderURL!,getHeaderURL2!,getHeaderURL3!,getHeaderURL4!]
print(imgArray)
}
my collection view functions:我的收藏视图功能:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print(imgArray.count)
print("this is the count of images")
return imgArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = imageCollectionView.dequeueReusableCell(withReuseIdentifier: "imgCell", for: indexPath) as! ImageCellViewController
cell.sliderImage.sd_setShowActivityIndicatorView(true)
cell.sliderImage.sd_setIndicatorStyle(.gray)
cell.sliderImage.sd_setImage(with: imgArray[indexPath.row], placeholderImage: UIImage(named: "Profile_avatar_placeholder_large"))
return cell
}
在您填充imgArray
的行之后,在您的集合视图上调用reloadData()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.