![](/img/trans.png)
[英]iOS + Firebase - Send data from view controller to detail view controller
[英]Send Firebase data from Main View Controller to Detailed View Controller
我能够创建类似于Instagram的UICollection View提要,但是我不确定如何选择单元格并转到更详细的视图控制器。 这是我的主视图控制器的外观。 “
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "details", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "details" {
if let indexPaths = self.collectionView!.indexPathsForSelectedItems{
let vc = segue.destination as! MainViewController
let indexPath = indexPaths[0] as NSIndexPath
let post = self.posts[indexPath.row] as! [String: AnyObject]
let Booked = post["title"] as? String
let Authors = post["Author"] as? String
let ISBNS = post["ISBN"] as? String
let Prices = post["Price"] as? String
let imageNames = post["image"] as? String
vc.Booked = Booked
vc.Authors = Authors
vc.ISBNS = ISBNS
vc.Prices = Prices
vc.imageNames = imageNames
print(indexPath.row)
} }}
这是我的数据库的样子:
//Detailed View Controller
FIRDatabase.database().reference().child("posts").child(self.loggedInUser!.uid).observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in
if let dictionary = snapshot .value as? [String: AnyObject] {
self.BookTitle.text = dictionary["title"] as? String
self.Author.text = dictionary["Author"] as? String
self.ISBN.text = dictionary["ISBN"] as? String
self.Price.text = dictionary["Price"] as? String
self.Image.image = ["image"] as? UIImage
}
})
上面是我的详细视图控制器。 但是,当我单击单元格时,我的信息没有通过
然后,如下所示修改您的代码:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// No Need to call this performSegue(withIdentifier: "details", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "details" {
if let indexPaths = self.collectionView!.indexPathsForSelectedItems{
let vc = segue.destination as! MainViewController
let cell = sender as UICollectionViewCell
let indexPath = self.collectionView!.indexPathForCell(cell)
let post = self.posts[indexPath.row] as! [String: AnyObject]
let Booked = post["title"] as? String
let Authors = post["Author"] as? String
let ISBNS = post["ISBN"] as? String
let Prices = post["Price"] as? String
let imageNames = post["image"] as? String
vc.Booked = Booked
vc.Authors = Authors
vc.ISBNS = ISBNS
vc.Prices = Prices
vc.imageNames = imageNames
print(indexPath.row)
} }}
然后在DetailsViewController中的代码将如下所示(无需再次引用firebase,因为您已经拥有所有信息):
self.BookTitle.text = self.Booked
self.Author.text = self.Author
self.ISBN.text = self.ISBN
self.Price.text = self.Price
if let stringImage = self.imageNames as? String {
let imageRef = storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/\(stringImage)")
imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in
if error == nil {
self.Image.image = UIImage(data: data!)
}else {
print("Error downloading image:" )
}
在viewDidLoad中编写代码。
这似乎是重复的。 在此处可以找到在视图控制器之间传递数据的一个很好的例子: 在视图控制器之间传递数据 。
构建数据也很重要。 如果您有从Firebase数据库收到的Post
对象,则应创建一个本地Post对象并引用该对象。 在UICollectionViewCell
,可以使用所选的indexPath来获取指定给该单元格的Post
。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "details" {
if let indexPaths = self.collectionView!.indexPathsForSelectedItems{
let vc = segue.destination as! MainViewController
let cell = sender as! UICollectionViewCell
let indexPath = self.collectionView!.indexPath(for: cell)
let post = self.posts[(indexPath?.row)!] as! [String: AnyObject]
let Booked = post["title"] as? String
let Authors = post["Author"] as? String
let ISBNS = post["ISBN"] as? String
let Prices = post["Price"] as? String
if let imageNames = post["image"] as? String {
let imageRef = storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/")
imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in
if error == nil {
let image = UIImage(data: data!)
}else {
print("Error downloading image:" )
}
vc.Booked = Booked
vc.Authors = Authors
vc.ISBNS = ISBNS
vc.Prices = Prices
vc.imageNames = imageNames
print(indexPath?.row)
})} } }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.