如何使用 firebase 从 collectionCell 获取不同数量的图片到 scrollView ? 在我的项目中,我有三个viewController的。
tableView 中的FirstVC - collectionView 。 CollectionView 需要显示主要图像以便滚动它们。 TableView 需要显示名称和滚动表格。 (即对于 collectionCell 中 FirstVC 中的图像)。
SecondVC - 仅 collectionView 。需要显示相册。
ThirdVC - viewController 中仅 scrollView 。 ScrollView 需要位于 firebase 的 images 文件夹中的滚动图片。
所以,当我点击 collectionCell 中的 secondVC 中的相册时,我想在 thirdVC 中看到< strong> scrollView 位于 firebase 中图像文件夹中的图像,用于滚动它们。 我的 firebase 结构位于:
{
"users" : {
"Y7EHcJuqQWZrVI1HBmNgccbPhm55" : {
"name" : "Photos",
"posts" : {
"images" : {
"image" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/gallery%2F06BAB847-7C94-40A6-A9C8-C3A11B3F9C81.png?alt=media&token=4f797093-fbc7-437b-935c-d13be1409d13",
"image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/gallery%2F06BAB847-7C94-40A6-A9C8-C3A11B3F9C81.png?alt=media&token=4f797093-fbc7-437b-935c-d13be1409d13"
},
"qwerty" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/gallery%2F06BAB847-7C94-40A6-A9C8-C3A11B3F9C81.png?alt=media&token=4f797093-fbc7-437b-935c-d13be1409d13"
},
"posts2" : {
"images" : {
"image" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/gallery%2F06BAB847-7C94-40A6-A9C8-C3A11B3F9C81.png?alt=media&token=4f797093-fbc7-437b-935c-d13be1409d13",
"image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/gallery%2F06BAB847-7C94-40A6-A9C8-C3A11B3F9C81.png?alt=media&token=4f797093-fbc7-437b-935c-d13be1409d13"
},
"qwerty" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/city2.jpg?alt=media&token=64509e18-9884-4449-a081-c67393a7d82a"
},
"posts3" : {
"images" : {
"image" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/gallery%2F06BAB847-7C94-40A6-A9C8-C3A11B3F9C81.png?alt=media&token=4f797093-fbc7-437b-935c-d13be1409d13",
"image1" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/gallery%2F06BAB847-7C94-40A6-A9C8-C3A11B3F9C81.png?alt=media&token=4f797093-fbc7-437b-935c-d13be1409d13",
"image2" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/gallery%2F06BAB847-7C94-40A6-A9C8-C3A11B3F9C81.png?alt=media&token=4f797093-fbc7-437b-935c-d13be1409d13"
},
"qwerty" : "https://firebasestorage.googleapis.com/v0/b/test2-29e4b.appspot.com/o/oko1.jpg?alt=media&token=fedea2cb-93b1-496c-9392-0b95f4ada7b5"
}
}
}
}
我的项目在 googleDrive ,因为我遇到了在 github 上加载项目的问题。我的项目非常简单,但我无法解决上面写的问题。
我的项目代码:
FirstVC :
import UIKit
import Firebase
import SDWebImage
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var nameArray = [String]()
var addressArray = [String]()
var postImage = [String](), postImage2 = [String](), postImage3 = [String]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
loadName()
loadImages()
tableView.allowsSelection = true
}
func loadName() {
FIRDatabase.database().reference().child("users").
observe(FIRDataEventType.childAdded, with: {(snapshot) in
let value = snapshot.value! as! NSDictionary
self.nameArray.append(value["name"] as? String ?? "")
})
}
func loadImages() {
FIRDatabase.database().reference().child("users").
observe(FIRDataEventType.childAdded, with: {(snapshot) in
let values = snapshot.value! as! NSDictionary
let posts = values["posts"] as? NSDictionary //[String: AnyObject]
let posts2 = values["posts2"] as? NSDictionary //[String: AnyObject]
let posts3 = values["posts3"] as? NSDictionary //[String: AnyObject]
self.postImage.append(posts?["qwerty"] as? String ?? "")
self.postImage2.append(posts2?["qwerty"] as? String ?? "")
self.postImage3.append(posts3?["qwerty"] as? String ?? "")
self.tableView.reloadData()
})
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue2" {
if let indexPaths = self.tableView.indexPathForSelectedRow {
let indexPath = indexPaths as NSIndexPath
let dvc = segue.destination as! CollectionViewController
let imagesArray = [self.postImage[indexPath.row],
self.postImage2[indexPath.row],
self.postImage3[indexPath.row]]
dvc.images = imagesArray.filter({ !($0.isEmpty) })
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
cell.userNameLabel.text = nameArray[indexPath.row]
let array = [postImage[indexPath.row],
postImage2[indexPath.row],
postImage3[indexPath.row]]
cell.postImageArray = array.filter({ !($0.isEmpty) })
return cell
}
}
SecondVC :
import UIKit
import SDWebImage
class CollectionViewController: UICollectionViewController {
var images = [String]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue3" {
if let indexPaths = self.collectionView?.indexPathsForSelectedItems {
let indexPath = indexPaths[0] as NSIndexPath
let dvc = segue.destination as! ViewControllerScrollView
dvc.images = [images[indexPath.item]]
}
}
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell2
cell.imageView.sd_setImage(with: URL(string: images[indexPath.item]))
return cell
}
}
ThirdVC :
import UIKit
class ViewControllerScrollView: UIViewController {
@IBOutlet weak var scrollView: UIScrollView!
var images = [String]()
override func viewDidLoad() {
super.viewDidLoad()
for i in 0..<images.count {
let imageView = UIImageView()
imageView.sd_setImage(with: URL(string: images[i]))
let xPosition = self.view.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.scrollView.frame.width, height: self.scrollView.frame.height)
scrollView.contentSize.width = scrollView.frame.width * CGFloat(i + 1)
scrollView.addSubview(imageView)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}