将PFQueryTableViewCell imageView设置为本地图像

时间:2015-05-15 05:52:46

标签: 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

那么如何根据Parse值设置单元格的图像(但我希望图像是本地图像)

1 个答案:

答案 0 :(得分:1)

您的手机对象有问题。首先,您最后不需要!标记。你应该这样:

let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! PFTableViewCell

在Xcode 6.3和Swift 1.2中你的下一行不能编译,因为你正在检查nil不能为零的东西 - 你强行解开单元格,所以它不再是一个可选的你不需要检查nil

请删除if cell == nil行并查看您的单元格是否在IB中正确设置(检查您是否设置了标识符)。