我已经按照developer.apple.com上的教程在Xcode中使用swift在表格视图中创建了一个配方应用程序。我已经开始转换为集合视图,但很难正确编写函数来展开和调整配方的数据更改。
有人可以帮忙调整代码吗?目前,它不喜欢console.time("positive");
(function() {
var size = 100000;
var v = 0;
while (size--) {
v+=(+size)%2
}
})();
console.timeEnd("positive");
console.time("negative");
(function() {
var size = 100000;
var v = 0;
while (size--) {
v+=(-size)%2
}
})();
console.timeEnd("negative");
成员.row。 selectedindexpath
saveMeals
[在此输入图像说明] [2]
//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()
}
}
这是膳食等级
//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)
} }
答案 0 :(得分:0)
好的,首先你需要了解这些collectionview属性,比如indexpath和stuff只能在collectionview的委托方法中使用,而不是在它们之外。
现在在您的代码中,您可以这样做。添加部分很容易。只需添加reloadData
来电
所以你将你的食物添加到你的数组列表中然后调用上面的方法
meals.append(meal)
saveMeals()
collectionView.reloadData()
棘手的部分是你如何编辑。因此,在编辑你可以做的事情是在你正在获得餐饮的价值,你还需要得到一些标识,用于编辑哪一餐,可能是餐饮ID。
然后在您的arrayList中,您只能编辑已编辑的ID的Meal对象,然后保存它。然后拨打collectionView.reloadData()
。
或者你也可以做的是当你进行编辑时,将indexpath的indexpath传递给下一个ViewController并将其设置为属性。当您执行展开segue时,从sourceViewController.selectedIndexPathProperty
之类的内容中获取该属性,然后在此处按照您目前使用它的方式使用它。