【发布时间】:2016-06-13 03:59:12
【问题描述】:
我是 Swift 和 iOS 开发的新手,还没有找到我的问题的具体答案。
我正在尝试使用 Core Data 填充具有 3 个标签和 2 个图像的自定义表格视图。
我阅读了可选项,甚至在故事板中创建单元格时使用字幕选项构建了类似于我现在尝试做的事情。这行得通。
我的核心数据设置在班级顶部如下所示:
// 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
}
我已经尝试了所有我能想到的排列方式,并在 Internet 和 StackOverflow 上搜索了一个具体的答案。我也不想使用字符串。我正在使用 imagePickerController。但即使在我的 imageAssets 中有图像,例如“定制食品”,“定制饮料”,图像仍然为零。
我破解不了!
【问题讨论】:
-
嗨,你的代码到底哪里出错了?您如何将图像保存到核心数据?您能否更新您的代码以显示自定义类的含义?图片需要保存为 NSData,你应该在使用它们之前打开你的选项。
-
您好 Daniel,感谢您的回复:代码在打开图像时失败。他们被发现为零。斯威夫特告诉我打开它们,例如cell.imageView?.image = UIImage(data: custom.customFourImages!)
-
但是每次都会崩溃。我有一个 CustomTableViewCell,其中 customLabel、customTwoLabel 等具有 IBOutlets,并且所有内容都连接在情节提要中。
-
我正在使用 TabBarController,每次单击自定义 tabBar 图标时,它都会崩溃,并且报告是“在展开时意外发现 nil”(lldb)。
-
这是我如何将图像保存在另一个名为“WorkViewController”的视图控制器中 do { try moc?.save() } catch { print("New Photo Failed") return } }
标签: swift image core-data tableview optional