将PFObject转换为cellForRowAtIndexPath中的子类的空值

时间:2016-06-16 14:45:01

标签: swift parse-platform pfsubclassing

我有一个问题就是将PFObject转换为我的Custom类名为" Customer"

这是班级的快速文件

    @objc class Customer: PFObject, PFSubclassing  {

    @NSManaged var CompanyName: String
    @NSManaged var City: String
    @NSManaged var CountryCode: String
    @NSManaged var Address: String
    @NSManaged var Prov: String
    @NSManaged var Email: String
    @NSManaged var Vat: String
    @NSManaged var PaymentDelay: String
    @NSManaged var ficId: String
    @NSManaged var owner: Owner


    override class func initialize() {

        struct Static {
            static var onceToken : dispatch_once_t = 0;
        }

        dispatch_once(&Static.onceToken){
            self.registerSubclass()
        }

    }

    static func parseClassName() -> String {
        return "Customer"
    }   
}

这是tableViewDelegate

@objc class CustomersViewController: PFQueryTableViewController {

    override func queryForTable() -> PFQuery {

        let query = PFQuery(className: "Customers")
        query.orderByAscending("CompanyName")
        query.includeKey("owner")

        return query
    }

    override func objectsDidLoad(error: NSError?) {
        super.objectsDidLoad(error)
        //print("\(objects?.count) customers")
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {

        let cell = cellForTableView(tableView)
        print("obj:\(object?.objectId)")

        if let customer = object as? Customer {
            print("customer")
            let customerLabel = cell.viewWithTag(101) as! UILabel
            customerLabel.text = customer.CompanyName
        }

        return cell;
    }

    func cellForTableView(tableView: UITableView) -> PFTableViewCell{

        let cellIdentifier = "CustomerCell"

        if let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? PFTableViewCell{
            return cell
        } else {
            return PFTableViewCell(style: .Subtitle, reuseIdentifier: cellIdentifier)
        }
    }
}

问题是这个代码永远不会执行......为什么?

if let customer = object as? Customer {
                print("customer")
                let customerLabel = cell.viewWithTag(101) as! UILabel
                customerLabel.text = customer.CompanyName
            }

这是我的错误还是PFSubclassing协议的错误?

日志说对象正确下载..这是一个包装问题吗?

  

OBJ:可选(" uZ1v4VTFnt&#34)   OBJ:可选(" ciCOtYFMif&#34)   OBJ:可选(" P43mV63o0l&#34)   OBJ:可选(" YhMWMkklwZ&#34)   OBJ:可选(" LtKxwHApKZ&#34)   OBJ:可选(" EXL5tEW9EI&#34)   OBJ:可选(" I9HoCWqYub&#34)

谢谢

1 个答案:

答案 0 :(得分:1)

查看班级名称不匹配

static func parseClassName() -> String {
    return "Customer"
}

let query = PFQuery(className: "Customers")

在查询中,您正在使用类名称Customers,而在解析类名称中,您将返回一个Customer

另外正如我在评论中提到的那样,你必须初始化app delegate中的parse对象