我是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。但即使我的图像资源中有图像,例如“定制食品”,“定制饮料”,图像仍然没有发现零。
我无法破解它!
嗨,你的代码到底在哪里?你如何将图像保存到核心数据?你能否更新你的代码来显示Custom类包含的内容?图像需要保存为NSData,并且在使用它们之前应打开您的可选项。 –
嗨丹尼尔,谢谢你的回复:代码无法解开图像。他们被发现无。斯威夫特告诉我拆开包装,例如cell.imageView?.image = UIImage(data:custom.customFourImages!) – slippies
但每次都崩溃。我有一个CustomTableViewCell,其中customLabel,customTwoLabel等具有IBOutlets,并且所有内容都连接在故事板中。 – slippies