当用户使用 swift 的核心数据点击索引 0 到 1 时,如何显示不同的照片过滤器选项?

Posted

技术标签:

【中文标题】当用户使用 swift 的核心数据点击索引 0 到 1 时,如何显示不同的照片过滤器选项?【英文标题】:How to show different photo filter options when user tapped on index 0 to 1 using core data with swift? 【发布时间】:2015-04-28 06:37:45 【问题描述】:

帮助我有两个 UICollectionView,第一个集合视图是用户可以点击它的标签,如果他们想要更多种类的过滤器,第二个集合视图是过滤器的位置。我试图显示 11 个过滤器当用户点击索引 0(这是我放在第一个集合视图上的第一个标签)和 7 个过滤器当用户点击索引 1(这是我放在第一个集合视图上的第二个标签)我不不知道为什么它不起作用,即使两个索引都被点击,它也只会给出相同的结果(7 个过滤器)。这是我正在处理的代码:这是我的核心数据类:

import Foundation

import CoreData

class CoreDataSeeder  var managedObjectContext: NSManagedObjectContext!

init(context: NSManagedObjectContext) 
//Look this up
self.managedObjectContext = context


func seedCoreData() 
var sepia = NSEntityDescription.insertNewObjectForEntityForName("Filter",      inManagedObjectContext: self.managedObjectContext) as! Filter
sepia.name = "CISepiaTone"

var gaussianBlur = NSEntityDescription.insertNewObjectForEntityForName("Filter", inManagedObjectContext: self.managedObjectContext) as! Filter
gaussianBlur.name = "CIGaussianBlur"
gaussianBlur.favorited = true

var colorMap = NSEntityDescription.insertNewObjectForEntityForName("Filter", inManagedObjectContext: self.managedObjectContext) as! Filter
colorMap.name = "CIColorMonochrome"

var gammaAdjust = NSEntityDescription.insertNewObjectForEntityForName("Filter", inManagedObjectContext: self.managedObjectContext) as! Filter
gammaAdjust.name = "CIColorMap"

var exposureAdjust = NSEntityDescription.insertNewObjectForEntityForName("Filter", inManagedObjectContext: self.managedObjectContext) as! Filter
exposureAdjust.name = "CIExposureAdjust"

var effectChrome = NSEntityDescription.insertNewObjectForEntityForName("Filter", inManagedObjectContext: self.managedObjectContext) as! Filter
effectChrome.name = "CIPhotoEffectChrome"

 var effectInstant = NSEntityDescription.insertNewObjectForEntityForName("Filter", inManagedObjectContext: self.managedObjectContext) as! Filter
effectInstant.name = "CIPhotoEffectInstant"

var effectMono = NSEntityDescription.insertNewObjectForEntityForName("Filter", inManagedObjectContext: self.managedObjectContext) as! Filter
effectMono.name = "CIPhotoEffectMono"

var effectNoir = NSEntityDescription.insertNewObjectForEntityForName("Filter", inManagedObjectContext: self.managedObjectContext) as! Filter
effectNoir.name = "CIPhotoEffectNoir"

var effectTransfer = NSEntityDescription.insertNewObjectForEntityForName("Filter", inManagedObjectContext: self.managedObjectContext) as! Filter
effectTransfer.name = "CIPhotoEffectTransfer"

var effectBloom = NSEntityDescription.insertNewObjectForEntityForName("Filter", inManagedObjectContext: self.managedObjectContext) as! Filter
effectBloom.name = "CIBloom"

var error: NSError?
//& is actually memory address, instead of pointer
self.managedObjectContext?.save(&error)

if error != nil 
    println(error?.localizedDescription)



func seedCoreDataTwo() 
var sepia = NSEntityDescription.insertNewObjectForEntityForName("Filter", inManagedObjectContext: self.managedObjectContext) as! Filter
sepia.name = "CISepiaTone"

var gaussianBlur = NSEntityDescription.insertNewObjectForEntityForName("Filter", inManagedObjectContext: self.managedObjectContext) as! Filter
gaussianBlur.name = "CIGaussianBlur"
gaussianBlur.favorited = true


var gammaAdjust = NSEntityDescription.insertNewObjectForEntityForName("Filter", inManagedObjectContext: self.managedObjectContext) as! Filter
gammaAdjust.name = "CIColorMap"

var effectMono = NSEntityDescription.insertNewObjectForEntityForName("Filter", inManagedObjectContext: self.managedObjectContext) as! Filter
effectMono.name = "CIPhotoEffectMono"

var effectNoir = NSEntityDescription.insertNewObjectForEntityForName("Filter", inManagedObjectContext: self.managedObjectContext) as! Filter
effectNoir.name = "CIPhotoEffectNoir"

var effectTransfer = NSEntityDescription.insertNewObjectForEntityForName("Filter", inManagedObjectContext: self.managedObjectContext) as! Filter
effectTransfer.name = "CIPhotoEffectTransfer"

var effectBloom = NSEntityDescription.insertNewObjectForEntityForName("Filter", inManagedObjectContext: self.managedObjectContext) as! Filter
effectBloom.name = "CIBloom"




var error: NSError?
//& is actually memory address, instead of pointer
self.managedObjectContext?.save(&error)

if error != nil 
    println(error?.localizedDescription)
 
 

 

视图控制器内部:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 
println("bosen kelima")
if (collectionView == self.collectionView) 
    println("First Content")
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("FILTER_CELL", forIndexPath: indexPath) as! FilterThumbnailCell
    var filterThumbnail = self.filterThumbnails[indexPath.row] // this one through bottom of this function will be deleted and moved to "didSelectItemAtIndexPath"
    cell.filterTitle.text = filterThumbnail.filterName
    //Lazy Loading
    if cell.viewWithTag(1) == nil 
    if filterThumbnail.filteredThumbnail != nil 
        cell.imageView.image = filterThumbnail.filteredThumbnail
     else 
        cell.imageView.image = filterThumbnail.originalThumbnail
        //filterThumbnail is a class instance
        filterThumbnail.generateThumbnail( (image) -> Void in
            if let cell = collectionView.cellForItemAtIndexPath(indexPath) as? FilterThumbnailCell 
                cell.imageView.image = image
            
        )
    
   
    return cell
   
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("DRUM", forIndexPath: indexPath) as! TextViewCell
cell.textOptionsLabel.text = textOptions[indexPath.row] // It will print Desire, Nature, Bloom
    return cell

  
   func collectionView(collectionView: UICollectionView,          shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool 
if (collectionView == self.collectionView)
let filteredImageSelected = self.filterThumbnails[indexPath.row] //One

//When user does not pick any other photo options, self.unFilteredImage is what is on screen.
if self.unFilteredImage == nil 
    self.unFilteredImage = self.imageView.image

 //Pass in unfiltered photo so filter will not keep building
    filteredImageSelected.filterImage(self.unFilteredImage!, completionHandler:  (filteredImage) -> Void in
        self.imageView!.image = filteredImage //Two
    )

    if (collectionView == self.secondCollectionView)
        println("shouldSelectItemAtIndexPath Clicked")
        if ((indexPath.row)==0)
            println("index number 1 Clicked")

           /* self.generateThumbnail()

            //Setting up GPU Context
            var options = [kCIContextWorkingColorSpace: NSNull()]
            var myEAGLContext = EAGLContext(API: EAGLRenderingAPI.OpenGLES2)
            self.context = CIContext(EAGLContext: myEAGLContext, options: options)*/

            var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            //Did this in CoreDataSeeder.swift
            self.managedObjectContext = appDelegate.managedObjectContext

            //Seeding Core Data
            var seeder = CoreDataSeeder(context: appDelegate.managedObjectContext!)

            let fetchRequest = NSFetchRequest(entityName: "Filter")
            var error: NSError?
            if let filters = self.managedObjectContext.executeFetchRequest(fetchRequest, error: &error) as? [Filter] 
                if filters.isEmpty 
                    seeder.seedCoreData()
                    self.filters = self.managedObjectContext.executeFetchRequest(fetchRequest, error: &error) as! [Filter]
                 else 
                    self.filters = filters
                
            
            println("Number of filters view did load: \(filters.count)")
            self.resetFilterThumbnails()
            self.collectionView.delegate = self //Try to make another collection view and data source to trigger
            self.collectionView.dataSource = self
        
if ((indexPath.row)==1) 
            self.generateThumbnail()

            //Setting up GPU Context
            var options = [kCIContextWorkingColorSpace: NSNull()]
            var myEAGLContext = EAGLContext(API: EAGLRenderingAPI.OpenGLES2)
            self.context = CIContext(EAGLContext: myEAGLContext, options: options)

            var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            //Did this in CoreDataSeeder.swift
            self.managedObjectContext = appDelegate.managedObjectContext

            //Seeding Core Data
            var seeder = CoreDataSeeder(context: appDelegate.managedObjectContext!)

            let fetchRequest = NSFetchRequest(entityName: "Filter")
            var error: NSError?
            if let filters = self.managedObjectContext.executeFetchRequest(fetchRequest, error: &error) as? [Filter] 
                if filters.isEmpty 
                    seeder.seedCoreDataTwo()
                    self.filters = self.managedObjectContext.executeFetchRequest(fetchRequest, error: &error) as! [Filter]
                 else 
                    self.filters = filters
                
            //Try find a way to move this to cellforindexpath

            println("Number of filters view did load: \(filters.count)")
            self.resetFilterThumbnails()
            self.collectionView.delegate = self //Try to make another collection view and data source to trigger
            self.collectionView.dataSource = self
        

    
return false

有没有办法让它工作?如果你能帮助我,我将不胜感激

【问题讨论】:

我不明白你为什么要使用 CoreData。你不需要它,尤其是当你不知道如何使用它的时候。只需使用 2 个带字符串的数组作为过滤器名称。 【参考方案1】:

您分两步将所有这些对象添加到 CoreData,但我认为您没有采取任何方法来分别检索它们。将它们添加到 2 个函数中并不重要,它们都去同一个地方。

您需要做的是为您的对象添加一个属性:说“组”,并将其设置为第一个组的 1 和第二个组的 2。然后,当您从 CoreData 获取它们以显示它们时,您可以根据您想要获取的组过滤它们。

【讨论】:

抱歉不完整,你可以再看一遍上面的代码@pteofil 我想我的错误是在索引 0 和 1 上编写相同的代码,除了我在索引 0 上调用 seeder.seedCoreData() 和在索引 1 上调用 seeder.seedCoreDataTwo() @pteofil

以上是关于当用户使用 swift 的核心数据点击索引 0 到 1 时,如何显示不同的照片过滤器选项?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Swift 在文本字段(从右到左)上输入货币格式?

如何使用 Swift 在文本字段(从右到左)上输入货币格式?

indexPath swift的索引超出范围

当用户使用 iOS 13 Swift 5 点击推送通知时,在特定视图中打开应用程序

带有核心数据的 TableView 中的搜索栏(使用 Swift)

Rails:使范围数据从索引到显示页面保持不变