简体   繁体   中英

empty value on cast PFObject to subclass in cellForRowAtIndexPath

i have a problem on cast an PFObject to my Custom class named "Customer"

this is swift file for the class

    @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"
    }   
}

and this is 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)
        }
    }
}

The problem is that this code is never executed... why?

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

It's an my error or a bug of PFSubclassing protocol?

The logs say the object are correctly download..it's a wrap problem?

obj:Optional("uZ1v4VTFnt") obj:Optional("ciCOtYFMif") obj:Optional("P43mV63o0l") obj:Optional("YhMWMkklwZ") obj:Optional("LtKxwHApKZ") obj:Optional("EXL5tEW9EI") obj:Optional("I9HoCWqYub")

Thank you

Have a look at class names mismatches

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

let query = PFQuery(className: "Customers")

In query you are using class name Customers while in parse class name you are returning a Customer

Also as I mention in the comment you have to initialize parse object in app delegate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM