ios - 将 PFQueryTableViewCell imageView 设置为本地镜像

标签 ios uitableview swift parse-platform

[使用 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/

相关文章:

ios - 未调用 subview 手势识别器

ios - 在 iOS MapView 中更改 map 颜色?

ios - SpriteKit 定位和坐标系

objective-c - 同步多个 UITableView 实例的滚动位置

ios - 缓存 URL 图片 iphone UITableview

iOS 如何从 UISlider 中删除发光/阴影

swift - 符合协议(protocol)——一次又一次地添加协议(protocol) stub 并不能修复错误

objective-c - 视网膜显示器的矩形尺寸是多少?

iphone - 我需要做什么来配置一个新的 iPhone 项目,以便它可以被视为与 Appstore 中我当前的 iPhone 应用程序相同?

ios - 如何使用 Stackview 消费整个 View