gpt4 book ai didi

ios - 核心数据的 Collection View - 最后选择的内容首先出现

转载 作者:行者123 更新时间:2023-11-29 01:41:59 25 4
gpt4 key购买 nike

当前功能

我有一个 Collection View ,其中每个单元格都由图像和标签组成。它是 View Controller 中的 Collection View 。目前,该 View 可以从两个数组(一个用于图像名称,一个用于标签名称)编译和加载。当用户点击一个单元格时,它会将他们带到一个新的 View Controller 。

期望的功能

但是,我想添加这样的功能,当用户返回到 Collection View 时,他们点击的单元格现在是第一个(左上角位置)。此新订单应保存到核心数据并成为新的默认订单。

迄今为止的进展

我已经开始尝试实现此功能,但事实证明将原始数组放入 CoreData 具有挑战性。我无法将原始数组放入 CoreData 中,也无法从 CoreData 提取中构建 collectionView。 Collection View 构建但我的图像/标签都不存在,只有一些 CoreData 代码加载到我的标签中......

这是我在 viewDidLoad 中得到的:

    //Fetch & sort the list from Core Data
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!
let fetchRequest = NSFetchRequest(entityName: "MenuItem_llcv")
let sortDescriptor = NSSortDescriptor(key: "displayOrder", ascending: true )
fetchRequest.sortDescriptors = [ sortDescriptor ]
var error: NSError?
let fetchedResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as? [NSManagedObject]

//Check fetch results
//If fetch was successful, the results are assigned to the context object
if let results = fetchedResults {
ll_ColViewOrder_Context = results
println("Fetch succesful, although possibly empty.")
} else {
println("Could not fetch \(error), \(error!.userInfo)")
}

//First-time user check
//If CoreData has the arrays, do nothing. If it doesn't, load them in.
if ll_ColViewOrder_Context.count > 8 {
println("Fetch result: Previously stored list found in Core Data.")
} else {
//Set arrays to populate cells
cvListNames = ["Label 1", "Label 2", "Label 3", "Label 4", "Label 5"]

cvListImgs = ["image1","image2","image3","image4","image5"]

let entityVar = NSEntityDescription.entityForName("MenuItem_llcv", inManagedObjectContext: managedContext)
let listItem = NSManagedObject(entity: entityVar!, insertIntoManagedObjectContext:managedContext)

var numberToStore = 1

for ea_Name in cvListNames {
listItem.setValue(ea_Name, forKey: "name")
}
for ea_ImgName in cvListImgs {
listItem.setValue(ea_ImgName, forKey: "imgName")
}
for ea_OrderNum in cvListImgs {
listItem.setValue(numberToStore, forKey: "displayOrder")
++numberToStore
}
managedContext.save(nil)
println("Context now contains \(ll_ColViewOrder_Context)")
}

这是我的表格构建的依据:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! LifeList_CollectionViewCell

//Assign images
let image = UIImage(named: "\(ll_ColViewOrder_Context[indexPath.row])")
cell.cvCell_Image.image = image

//Assign list names
let title = "\(ll_ColViewOrder_Context[indexPath.row])"
cell.cvCell_Name.text = title

最佳答案

您只创建了一个 NSManagedObject,然后通过为它的每个属性分配值来迭代。但你仍然只有一个对象。您需要为数组中的每个值创建一个新的 NSMO:

    //Set arrays to populate cells
cvListNames = ["Label 1", "Label 2", "Label 3", "Label 4", "Label 5"]
cvListImgs = ["image1","image2","image3","image4","image5"]

let entityVar = NSEntityDescription.entityForName("MenuItem_llcv", inManagedObjectContext: managedContext)

for (index, value) in enumerate(cvListNames) {
let listItem = NSManagedObject(entity: entityVar!, insertIntoManagedObjectContext:managedContext)
listItem.setValue(value, forKey: "name")
let imageName = cvListImgs[index]
listItem.setValue(imageName, forKey: "imgName")
listItem.setValue(index, forKey: "displayOrder")
ll_ColViewOrder_Context.append(listItem)
}
managedContext.save(nil)
println("Context now contains \(ll_ColViewOrder_Context)")

我使用了 enumerate 函数来获取 cvListNames 中每个值的 index。然后可以使用此 indexcvListImgs 中查找相应的图像,并填充 displayOrder 属性。我还添加了一行代码,用于将每个新的 NSMO 附加到支持您的 Collection View 的数组。

此外,在填充 Collection View 的代码中,您需要使用valueForKey 来提取相关属性的值:

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! LifeList_CollectionViewCell
//Assign images
let currentObject = ll_ColViewOrder_Context[indexPath.row]
let imageName = currentObject.valueForKey("imgName")
let listName = currentObject.valueForKey("name")
let image = UIImage(named: imageName)
cell.cvCell_Image.image = image
//Assign list names
cell.cvCell_Name.text = listName

关于ios - 核心数据的 Collection View - 最后选择的内容首先出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32295717/

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