2016-06-13 83 views
0

我是Swift和iOS开发人员的新手,并且尚未找到针对我的问题的特定答案。在Swift中使用自定义TableViewCell的核心数据图像

我正在尝试使用Core Data填充3个标签和2个图像的自定义表格视图。

我阅读了可选项,甚至在创建故事板中的单元格时使用Subtitle选项创建了类似于我现在尝试执行的操作。这样可行。

我的核心数据设置看起来像这样在类的顶部:

// MARK: - Managed Object Context 

var moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext 

// Initialize Fetch Results 

var fetchedResultsController = NSFetchedResultsController() 

func fetchRequest() -> NSFetchRequest { 

    let fetchRequest = NSFetchRequest(entityName: "Custom") 
    let sortDescriptor = NSSortDescriptor(key: "customFourImages", ascending: true) 

    fetchRequest.sortDescriptors = [sortDescriptor] 

    return fetchRequest 
} 

// MARK: - Retrieve Request 

func getFetchRequest() -> NSFetchedResultsController { 

    fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest(), managedObjectContext: moc!, sectionNameKeyPath: nil, cacheName: nil) 

    return fetchedResultsController 
} 

我viewDidLoad中和viewDidAppear是这样的:

// MARK: - Fetch 

    fetchedResultsController = getFetchRequest() 
    fetchedResultsController.delegate = self 

    do { 
     try fetchedResultsController.performFetch() 
    } catch { 
     print("Failed to Perform Initial Fetch") 
     return 
    } 
    self.tableView.reloadData() 

最后,我的cellForRowAtIndexPath看起来是这样的:

// MARK: - Dequeue Reusable Cell 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell 

    // MARK: - Initiate Fetch Results for Index Path 

    let custom = fetchedResultsController.objectAtIndexPath(indexPath) as! Custom 

    let customLabel = custom.customLabels 
    let customTwoLabel = custom.customTwoLabels 
    let customThreeLabel = custom.customThreeLabels 

    let image = UIImage(named: "Custom Food") 
    let imageData = UIImagePNGRepresentation(image!) 
    custom.customFourImages = imageData 

    let images = UIImage(named: "Custom Drink") 
    let imagesData = UIImagePNGRepresentation(images!) 
    custom.customFiveImages = imagesData 

    // MARK: - Set Items in Cell to Returned Fetched Results 

    cell.customLabel?.text = "\(customLabel)" 
    cell.customTwoLabel?.text = "\(customTwoLabel)" 
    cell.customThreeLabel?.text = "\(customThreeLabel)" 
    cell.imageView?.image = UIImage(data: custom.customFourImages!) 
    cell.imageView?.image = UIImage(data: custom.customFiveImages!) 

    return cell 
} 

我尝试过每一个我能想到的排列方式, ernet和StackOverflow作为特定的答案。我也不喜欢使用字符串。我正在使用imagePickerController。但即使我的图像资源中有图像,例如“定制食品”,“定制饮料”,图像仍然没有发现零。

我无法破解它!

+0

嗨,你的代码到底在哪里?你如何将图像保存到核心数据?你能否更新你的代码来显示Custom类包含的内容?图像需要保存为NSData,并且在使用它们之前应打开您的可选项。 –

+0

嗨丹尼尔,谢谢你的回复:代码无法解开图像。他们被发现无。斯威夫特告诉我拆开包装,例如cell.imageView?.image = UIImage(data:custom.customFourImages!) – slippies

+0

但每次都崩溃。我有一个CustomTableViewCell,其中customLabel,customTwoLabel等具有IBOutlets,并且所有内容都连接在故事板中。 – slippies

回答

1

从您的意见我不能锻炼你实际上保存你的托管对象到核心数据。你提到你使用了try.moc?.save(),但你是否将你的个人图像保存到核心数据?此外,如果您的图像本地存储在设备中,而不是从URL中获取,那么您并不需要核心数据。

在您从一个URL或任何抓取您的图片,并假设你已经添加的实体MYIMAGE类型的NSData的的imageData性的情况下,你需要保存的对象,像这样:

class func createInManagedObjectContext(moc: NSManagedObjectContext, imgData: NSData) ->MyImage { 
    let newItem = NSEntityDescription.insertNewObjectForEntityForName("MyImage", inManagedObjectContext: moc) as! MyImage 
    newItem.imageData = imagData 
    return newItem 
} 

该功能添加到您的MyImage.Swift文件(核心数据生成),这样,当你需要保存到核心数据的新形象,你只是做:

func saveNewImage(data: {DATA}){ 
    MyImage.createInManagedObjectContext(self.managedObjectContext, imgData: data); 
} 

然后,获取您的对象:

func getSavedImages() -> [MyImage]? { 
    fetchRequest.sortDescriptors = [sortDescriptor] 

    do { 
     return try self.managedObjectContext.executeFetchRequest(fetchRequest) as? [MyImage] 
    } catch { 
     print("no records found") 
     return nil 
    } 
} 

如果你已经这样做了,能否请您提供更多详情更新代码?指出你的错误发生的地方(行号)。

如果你的图片是失败的位置:

cell.imageView?.image = UIImage(data: custom.customFourImages!) 
cell.imageView?.image = UIImage(data: custom.customFiveImages!) 

中的抓取结果不会从核心数据,这可能是与上面的问题得到记录。

如果你的代码失败在这里:

let images = UIImage(named: "Custom Drink") 
let imagesData = UIImagePNGRepresentation(images!) 

你应该unwraping您的图像与一个对象,如果让IMG =图像{...}并且很有可能这个错误与你从内存中获得的UIImage的名字有关。

我希望这会有所帮助。

干杯。

编辑1: 在回答您的意见: try.moc.save不会保存您的图像,它会持续在数据模型中挂起的任何更改。

您需要实际在核心数据中创建托管对象,并且需要确保您的提取请求正在从核心数据中获取相关数据。

您的代码失败,因为customFourImages是零。哪一个来自:

let image = UIImage(named: "Custom Food") 
let imageData = UIImagePNGRepresentation(image!) 
custom.customFourImages = imageData 

.... 

let images = UIImage(named: "Custom Drink") 
let imagesData = UIImagePNGRepresentation(images!) 
custom.customFiveImages = imagesData 

... 

cell.imageView?.image = UIImage(data: custom.customFourImages!) 
cell.imageView?.image = UIImage(data: custom.customFiveImages!) 

这一切似乎有点多余。

试试这个:

let image = UIImage(named: "Custom Food") 
let imageData = UIImagePNGRepresentation(image!) 
cell.imageView?.image = image 
custom.createInManagedObjectContext(moc, imgData: imageData) 
.... 

let images = UIImage(named: "Custom Drink") 
let imagesData = UIImagePNGRepresentation(images!) 
cell.imageView?.image = images //this is called in the same block? you are overwriting your imageView in the cell, maybe you have two? 
custom.createInManagedObjectContext(moc, imgData: imagesData) 

这将图像保存到核心数据和右侧图像分配给你的看法(我不确定这是否会解决保存图像数据,核心数据的您的需求) 。确保您明白为什么需要使用核心数据并保存适合您要求的适当管理对象。

+0

!我将这些新功能添加到由Core Data创建的Custom.swift文件中。该表正在加载,我可以使用UITextFields从我的第二个视图控制器发布customLabel,customLabelTwo,customLabelThree的值。但我仍然无法获取图像。提醒:我正在使用imagePickerController。 – slippies

+0

是的。图像存储在本地设备上,并具有相机功能。是的,我允许外部存储,我认为这就是你的意思,因为我可以使用NSFileManager。 – slippies

+0

如果您的所有图像都存储在本地,为什么您需要核心数据存储您的图像? –