我正在使用Firebase数据库将数据保存在那里。我可以成功保存所有项目,并且基本上可以在数据库控制台中看到它们。我还可以用存储的数据填充collectionView单元。
唯一的问题是我无法成功将数据传递到详细信息视图。
我看过我的另一个iOS应用程序,在该应用程序中我成功地从tableview筛选到了视图控制器,但无法使其与此新应用程序中的collectionView一起使用。
这是从Firebase数据库获取数据的视图控制器。 observeRecipes()函数在控制台中打印出数据。
class RecipesViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var recipesCollectionView: UICollectionView!
var ref: DatabaseReference!
var recipes = [Recipe]()
override func viewDidLoad() {
super.viewDidLoad()
recipesCollectionView.delegate = self
recipesCollectionView.dataSource = self
observeRecipes()
IHProgressHUD.showImage(UIImage(named:"disabled")!, status: "Loading")
}
func observeRecipes() {
let ref = Database.database().reference(withPath: "recipes")
ref.observe(.value, with: { snapshot in
print(snapshot.value as Any)
var newRecipes: [Recipe] = []
for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String:Any],
let showTitle = dict["RecipeName"] as? String,
let showTime = dict["RecipeTime"] as? String,
let recipeImage = dict["RecipePhoto"] as? String,
let ingridients = dict["Ingridients"] as? String,
let firstImageViewURL = dict["imageOne"] as? String,
let firstIngridients = dict["firstIngridientList"] as? String,
let firstInstructions = dict["firstInstructionList"] as? String,
let secondImageViewURL = dict["imageTwo"] as? String,
let secondIngridients = dict["secondIngridientList"] as? String,
let secondInstructions = dict["secondInstructionList"] as? String,
let thirdImageViewURL = dict["imageThird"] as? String,
let thirdIngridients = dict["thirdIngridientList"] as? String,
let thirdInstructions = dict["thirdInstructionList"] as? String,
let url = URL(string:recipeImage),
let urlFirst = URL(string: firstImageViewURL),
let urlSecond = URL(string: secondImageViewURL),
let urlThird = URL(string: thirdImageViewURL) {
let recipe = Recipe(title: showTitle, time: showTime, recipeImageURL: url, ingridients: ingridients, firstImageViewURL: urlFirst, firstIngridients: firstIngridients, firstInstructions: firstInstructions, secondImageViewURL: urlSecond, secondIngridients: secondIngridients, secondInstructions: secondInstructions, thirdImageViewURL: urlThird, thirdIngridients: thirdIngridients, thirdInstructions: thirdInstructions)
newRecipes.append(recipe)
}
}
self.recipes = newRecipes
self.recipesCollectionView.reloadData()
})
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return recipes.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let recipeCell = collectionView.dequeueReusableCell(withReuseIdentifier: "recipeCell", for: indexPath) as! RecipeCollectionViewCell
recipeCell.set(cell: recipes[indexPath.row])
return recipeCell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showRecipeDetails" {
let destination = segue.destination as! RecipeDetailViewController
destination.selectedRecipe = sender as! Recipe
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = recipesCollectionView.cellForItem(at: indexPath) as! RecipeCollectionViewCell
performSegue(withIdentifier: "showRecipeDetails", sender: cell)
// performSegue(withIdentifier: "showRecipeDetails", sender: indexPath)
}
}
}
这是detailViewController
class RecipeDetailViewController: UIViewController, UIScrollViewDelegate {
var recipes = [Recipe]()
var ref: DatabaseReference?
var selectedRecipe: Recipe?
// Recipe Details
@IBOutlet weak var recipeTitleLabel: UILabel!
@IBOutlet weak var recipeTimeLabel: UILabel!
@IBOutlet weak var recipeImageView: UIImageView!
@IBOutlet weak var ingridientsLabel: UILabel!
//General Outlets & Variables
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var closeButtonView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
recipeTitleLabel.text = selectedRecipe?.title
}
下面是我在其他应用程序中一直用于prepareForSegue的代码,可以正常工作。我试图对此进行调整以使其适合collectionView,但是以某种方式我无法使其正常工作。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetails" {
let destination = segue.destination as! StoryDetailsViewController
if let indexPath = self.tableView.indexPathForSelectedRow {
destination.selectedStory = self.posts[indexPath.row]
print(indexPath.row)
}
}
}
答案 0 :(得分:0)
您可以尝试
performSegue(withIdentifier: "showRecipeDetails", sender: recipes[indexPath.item])
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showRecipeDetails" {
let destination = segue.destination as! RecipeDetailViewController
destination.selectedRecipe = sender as! Recipe
}
}