我在cloudkit DB中有一个可选映像(已检查的DB,如果在测试中添加了该映像,则该映像在那里)。我创建了一个将记录字段初始化为在tableview中使用的变量的类。我也有一个自定义单元。但是图像不会显示在我的自定义表格视图单元格中。我不知道tableview图像中是否包含可选图像会导致问题,或者我在cloudkit中的代码/设置是否存在问题。
非常感谢您的帮助,因为我已经呆了一个多星期,网上对此几乎没有。
这是我的课程代码:
var feedResults = [UserPosts]()
class UserPosts {
var record: CKRecord
var description: String
var username: String
//var image: CKAsset? // tried also initializing the CKAsset separately
var postPic: UIImage?
init(record: CKRecord) {
self.record = record
self.description = record.objectForKey("description") as String
self.username = record.objectForKey("username") as String
// self.image = record.objectForKey("postPic") as? CKAsset
if var pic = record.objectForKey("postPic") as CKAsset! {
self.postPic = UIImage(contentsOfFile: pic.fileURL.path!)
// below is the other way I've seen it done.
// var url = pic.fileURL!
// var imageData = NSData(contentsOfFile: url.path!)
// self.postPic = UIImage(data: imageData!)
}
}
}
这是我的tableview代码:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell
let record = feedResults[indexPath.row]
cell.postDescription.text = record.description
cell.userName.text = record.username
if record.postPic != nil {
cell.postPic.image = record.postPic!
}
return cell
}
我也看到了几种将CKAset拉入UIImage的方法。第一种方法是我在Apple的CloudKitAtlas项目中看到苹果这样做的方式。尽管我不太熟悉Obj-C,但我认为我正确地遵循了它-CloudKitAtlas示例
我也看到它是使用NSData(contentOFFile :)和UIImage(data :)完成的,如此处的帖子所示:SO CKAsset示例
尝试不使用.paths并使用contentsOfURL。这是我的操作方式(在您的CKAset中):
if var data = NSData(contentsOfURL: pic!.fileURL) {
self.postPic = UIImage(data: data!)!
}
除此之外,您可以执行dequeueReusableCellWithIdentifier,但是不要检查它是否返回nil。您应该创建一个新的单元格实例(如果为零)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句