I want to list all favorite ServiceProviders from the current user. Everything is working right except the table view: its duplicating the last object.
There is a class Favorite: objectId, User (pointer to User), ServiceProvider (pointer to ServiceProvider).
func baseQuery() -> PFQuery {
let favoriteQuery = PFQuery(className: "Favorite")
favoriteQuery.whereKey("favoriteUser", equalTo:PFUser.currentUser()!)
favoriteQuery.includeKey("favoriteServiceProvider")
favoriteQuery.orderByDescending("createdAt")
return favoriteQuery
}
override func queryForTable() -> PFQuery {
return self.baseQuery().fromLocalDatastore()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects!.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("favoriteCell") as! FavoriteTableViewCell!
if cell == nil {
cell = FavoriteTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "favoriteCell")
}
print("Founded \(objects!.count) favorite(s) from subquery")
//print(objects)
for object in objects! {
if let favoriteServiceProvider = object["favoriteServiceProvider"] as? PFObject {
let varspName = favoriteServiceProvider["spName"] as? String
print("spName: \(varspName)")
cell.labelspName.text = varspName
let varspCity = favoriteServiceProvider["spCity"] as? String
print("spCity: \(varspCity)")
cell.labelspCity.text = varspCity
let pictureUser: PFFile = (favoriteServiceProvider["spUserPhoto"] as? PFFile)!
print("spServiceProviderPhoto: \(pictureUser)")
pictureUser.getDataInBackgroundWithBlock { (data: NSData?, err: NSError?) -> Void in
if data != nil{
//Rounded spPhoto
cell.imageViewUserPhoto.image = UIImage(data: data!)
cell.imageViewUserPhoto.layer.cornerRadius = cell.imageViewUserPhoto.frame.size.width / 2
cell.imageViewUserPhoto.clipsToBounds = true
cell.imageViewUserPhoto.layer.borderColor = UIColor.lightGrayColor().CGColor
cell.imageViewUserPhoto.layer.borderWidth = 2
}
}
let varRating = favoriteServiceProvider["spAverageRating"] as? Int
print("spAverageRating: \(varRating)")
cell.ratingControl.rating = varRating!
}
}
return cell
}
I can found 2 favorites and it is ok!
And can see the 'prints' correctly.
spName: Optional("Service Provider Name")
spCity: Optional("Florianópolis / SC")
spServiceProviderPhoto: <PFFile: 0x12fde9de0>
spAverageRating: Optional(5)
spName: Optional("Service Provider Name 2")
spCity: Optional("Florianópolis / SC")
spServiceProviderPhoto: <PFFile: 0x12ff7ca60>
spAverageRating: Optional(4)
Everything looks like to be ok, but ... the app is repeating only the last object (showing two times the same object). What is wrong?