2016-06-13 82 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

如果您的所有圖像都存儲在本地,爲什麼您需要核心數據存儲您的圖像? –