为什么我的tableView重复最后一个对象? PFQuery使用指针

时间:2016-02-09 21:47:10

标签: swift pointers tableview relation pfquery

我想从当前用户列出所有喜爱的ServiceProviders。除了表视图之外,一切正常:它复制了最后一个对象。

有一个收藏夹:objectId,User(指向User的指针),ServiceProvider(指向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

}

我可以找到2个收藏夹,没关系!

可以正确看到'打印'。

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)

一切看起来都不错,但是......应用程序只重复最后一个对象(显示两次相同的对象)。有什么问题?

1 个答案:

答案 0 :(得分:1)

您正在遍历每个单元格的objects集合。因此,每个单元格都填充了最后一个单元格的值。相反,你应该编制索引:

let object = objects[indexPath.row]

此外,dequeueReusableCellWithIdentifier始终返回一个单元格,因此无需检查nil。