使用firebase从collectcell获取不同数量的图像 [英] Get a different numbers of images from collectioncell with use firebase
问题描述
如何使用 firebase 从 collectionCell 获取不同数量的图像到 scrollView ?在我的项目中,我有三个 viewController的。
FirstVC - collectionView 的tableView 。 CollectionView 需要显示滚动的主图像。 TableView 需要显示名称和滚动表格。 (即对于那些在 collectionCell 中 FirstVC 的图像)。
SecondVC - 仅 collectionView 。需要显示专辑。
ThirdVC - viewController 中只有 scrollView 。 ScrollView 需要滚动图像,它们位于 firebase 的 images 文件夹中。
所以当我点击 collectionCell 上的 secondVC 中的相册时,我想在 scrollView 中看到第三个VC位于 firebase 中的文件夹图像中的图像,用于滚动它们。我的firebase结构在这里:
How can i get a different numbers of images from collectionCell into scrollView with use firebase? In my project i have three viewController's.
FirstVC - collectionView inside a tableView. CollectionView need for display the main images for scroll their. TableView need for display the name and and to scroll the table. (i.e. for those images which are in FirstVC in collectionCell).
SecondVC - only collectionView. Need for display the albums.
ThirdVC - only scrollView inside viewController. ScrollView need for a scroll images which are located in the images folder in firebase.
So when i click on album in secondVC on collectionCell i would like to see in thirdVC in scrollView images which are located in folder images in firebase for scrolling their. My firebase struct is here:
{
"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"
}
}
}
}
我在图像分支中找到了 firstVC 图像,我将它们显示在打印件上,但我该怎么办呢接下来我无法理解。
我的 FirstVC :
I got on the firstVC the images in the images branch, I have them displayed on the print, but what do i do is next i can't understand.
My 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 ?? "")
if let post1 = posts as? [String: AnyObject] {
for (_, value) in post1["images"] as! [String: AnyObject] {
self.postImage.append(value as! String)
}
}
if let post2 = posts2 as? [String: AnyObject] {
for (_, value) in post2["images"] as! [String: AnyObject] {
self.postImage2.append(value as! String)
}
}
if let post3 = posts3 as? [String: AnyObject] {
for (_, value) in post3["images"] as! [String: AnyObject] {
self.postImage3.append(value as! String)
}
}
print(self.postImage3)
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 :
My 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 :
My 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()
}
}
我有什么尝试过:
未尝试,我的代码中的所有内容
What I have tried:
Not tried, everything inside my code
推荐答案
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) }) } } } 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)})
返回单元格
}
}
0.isEmpty) }) return cell } }
我的 SecondVC :
My 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 :
My 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()
}
}
我有什么尝试过:
未尝试,我的代码中的所有内容
What I have tried:
Not tried, everything inside my code
这篇关于使用firebase从collectcell获取不同数量的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!