gpt4 book ai didi

ios - 展开 Segue 以保存到 Collection View 中

转载 作者:行者123 更新时间:2023-11-30 12:14:54 24 4
gpt4 key购买 nike

我按照developer.apple.com上的教程在Xcode中使用表格 View 中的swift创建了一个菜谱应用程序。我已经开始转换为 Collection View ,但很难正确编码函数以展开和保存食谱的数据更改。

有人可以帮忙调整一下代码吗?目前,它不喜欢 selectedindexpath 的成员为 .row。已经有 saveMeals

的函数

错误

Error

//MARK: Actions
@IBAction func unwindToMealList(sender: UIStoryboardSegue) {
if let sourceViewController = sender.source as? MealViewController, let meal = sourceViewController.meal {

if let selectedIndexPath = collectionView.indexPathForSelectedRow {
// Update an existing meal.
meals[selectedIndexPath.row] = meal
collectionView.reloadRows(at: [selectedIndexPath], with: .none)
}
else {
// Add a new meal.
let newIndexPath = IndexPath(row: meals.count, section: 0)

meals.append(meal)
collectionView.insertRows(at: [newIndexPath], with: .automatic)
}
// Save the meals.
saveMeals()
}
}

[在此处输入图像描述][2]

//MARK: Actions
@IBAction func unwindToMealList(sender: UIStoryboardSegue) {
if let sourceViewController = sender.source as? MealViewController, let meal = sourceViewController.meal {

if let selectedIndexPath = collectionView?.numberOfSections{
// Update an existing meal.
meals[selectedIndexPath] = meal
collectionView?.reloadData()
}
else {
// Add a new meal.
meals.append(meal)
saveMeals()
collectionView?.reloadData()

}
}




}

这是餐课

import UIKit
import os.log

class Meal: NSObject, NSCoding {

//MARK: Properties

var name: String
var photo: UIImage?
var method: String?
var ingredients: String?



//MARK: Archiving Paths

static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("meals")


//MARK: Types

struct PropertyKey {
static let name = "name"
static let photo = "photo"
static let method = "method"
static let ingredients = "ingredients"


}

//MARK: Initialization

init?(name: String, photo: UIImage?, method: String?, ingredients: String?) {

// The name must not be empty
guard !name.isEmpty else {
return nil
}



// Initialize stored properties.
self.name = name
self.photo = photo
self.method = method
self.ingredients = ingredients


}

//MARK: NSCoding
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: PropertyKey.name)
aCoder.encode(photo, forKey: PropertyKey.photo)
aCoder.encode(method, forKey: PropertyKey.method)
aCoder.encode(ingredients, forKey: PropertyKey.ingredients)

}

required convenience init?(coder aDecoder: NSCoder) {

// The name is required. If we cannot decode a name string, the initializer should fail.
guard let name = aDecoder.decodeObject(forKey: PropertyKey.name) as? String else {
os_log("Unable to decode the name for a Meal object.", log: OSLog.default, type: .debug)
return nil
}

// Because photo is an optional property of Meal, just use conditional cast.
let photo = aDecoder.decodeObject(forKey: PropertyKey.photo) as? UIImage

let method = aDecoder.decodeObject(forKey: PropertyKey.method) as? String

let ingredients = aDecoder.decodeObject(forKey: PropertyKey.ingredients) as? String



// Must call designated initializer.
self.init(name: name, photo: photo, method: method, ingredients: ingredients)

} }

导航

  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
switch(segue.identifier ?? "") {
case "AddItem":
os_log("Adding a new meal.", log: OSLog.default, type: .debug)
case "ShowDetail":
guard let mealDetailViewController = segue.destination as? MealViewController else {
fatalError("Unexpected destination: \(segue.destination)")
}

guard let selectedMealCell = sender as? MealCollectionViewCell else {
fatalError("Unexpected sender: \(String(describing: sender))")
}

guard let indexPath = collectionView?.indexPath(for: selectedMealCell) else {
fatalError("The selected cell is not being displayed by the table")
}

let selectedMeal = meals[indexPath.row]
mealDetailViewController.meal = selectedMeal


default:
fatalError("Unexpected Segue Identifier; \(segue.identifier)")

}

最佳答案

好吧,首先您需要了解这些 CollectionView 属性(如索引路径和内容)仅在 CollectionView 的委托(delegate)方法内部可用,而不能在其外部使用。

现在在您的代码中您可以这样做。添加部分很简单。只需添加 reloadData 调用

因此,您将膳食添加到数组列表中,然后调用上面的方法

meals.append(meal)
saveMeals()
collectionView.reloadData()

棘手的部分是如何编辑。因此,在编辑中,您可以做的是在您的展开 Segue 中获取餐食的值,您还需要获取已编辑的餐食的一些标识符,可能是餐食 ID 或其他内容。

然后在你的arrayList中你可以只编辑已经编辑过的id的Meal对象,然后保存它。然后调用collectionView.reloadData()

或者您也可以在进行编辑时将索引路径从 itemSelected 传递到下一个 ViewController 并将其设置为属性。当您执行展开转场时,从 sourceViewController.selectedIndexPathProperty 等获取该属性,然后按照您目前使用的方式在此处使用它。

关于ios - 展开 Segue 以保存到 Collection View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45559894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com