[使用 Swift 为 iOS 开发]
我从 Parse 中提取数据并用它填充 PFQueryTableView,并且基于我用来填充 tableView 的 PFObjects 的值之一,我想将单元格的 imageView 设置为本地存储的图像在我的 Xcode images.xcassets 中。
这是我的代码:
// Set cells for each row of table
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! PFTableViewCell!
if cell == nil {
cell = PFTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
}
// Set image as read/unread
if let readStatus = object!["readStatus"] as? String {
if readStatus == "unread" {
cell.imageView?.image = UIImage(named: "unreadImage")
} else if readStatus == "read" {
cell.imageView?.image = UIImage(named: "readImage")
}
}
return cell
}
但它并没有设置图片...
如果我用 !在 imageView 之后(像这样):cell.imageView!.image = UIImage(named: "readImage")
我得到错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
那么如何根据解析值设置单元格的图像(但我希望图像是本地镜像)
最佳答案
您的单元格对象有问题。首先,你不需要 !
标记在末尾。你应该像这样:
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! PFTableViewCell
在 Xcode 6.3 和 Swift 1.2 中,你的下一行不会编译,因为你正在检查 nil 一些不能为 nil 的东西 - 你强制展开单元格所以它不再是可选的,你不需要检查 nil .
因此删除 if cell == nil
行并查看您的单元格是否在 IB 中正确设置(检查您是否设置了标识符)。
关于ios - 将 PFQueryTableViewCell imageView 设置为本地镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30252442/