getting total posts from parse using swift2

旧时模样 提交于 2019-12-12 03:59:41

问题


Every-time I open the app, it returns 6 rows, even if I created a new user with 1 post, it returns 6 rows. plus when I pull to refresh the data remains the same, I have to reopen the app again to see the new data added. this is my code below,

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    if PFUser.currentUser()?.objectId == nil{
        PFUser.currentUser()?.saveInBackgroundWithBlock({ (success, error) -> Void in
            let query = PFQuery(className: "JobPost")
            let userPointer = PFUser.objectWithoutDataWithObjectId(PFUser.currentUser()?.objectId)
            query.whereKey("postedBy", equalTo: userPointer)
            let objects = query.findObjects()
            self.dataSourceAnyObject.append(objects!)
        })
    } else {
        let query = PFQuery(className: "JobPost")
        let userPointer = PFUser.objectWithoutDataWithObjectId(PFUser.currentUser()?.objectId)
        query.whereKey("postedBy", equalTo: userPointer)
        let objects = query.findObjects()
        self.dataSourceAnyObject.append(objects!)
    }
    print("Data's in table =\(dataSourceAnyObject.count)")
    return dataSourceAnyObject.count
}

Here is the inside cell

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "EmpPostTVCellIdentifier"
    let cell: EmpPostTVCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as? EmpPostTVCell

    let query = PFQuery(className: "JobPost")

    //creating a pointer
    var userPointer = PFUser.objectWithoutDataWithObjectId(PFUser.currentUser()?.objectId)

    query.whereKey("postedBy", equalTo: userPointer)

    query.orderByDescending("createdAt")
    let objects = query.findObjects()
    for object in (objects as? [PFObject])!{
        //print(object.objectId)
        self.dataSource.append(object)
        self.createdByDate.append((object.objectForKey("closingDate") as? NSDate)!)
        print(dataSource)
        print(createdByDate)
    }

    if dataSource.isEmpty{
        print("no posts")
    }else{
    let itemArr:PFObject = self.dataSource[indexPath.row] as! PFObject
    cell?.companyPostLabel.text = (PFUser.currentUser()?.objectForKey("companyName")!.capitalizedString)! as String
    cell?.occupationPostLabel.text = itemArr["occupation"]!.capitalizedString as! String
    cell?.countryPostLabel.text = itemArr["country"]!.capitalizedString as String
    let companyImage: PFFile?
    companyImage = PFUser.currentUser()?.objectForKey("profileImageEmployer") as! PFFile
    companyImage?.getDataInBackgroundWithBlock({ (data, error) -> Void in
        if error == nil{
            cell?.companyLogoImage.image = UIImage(data: data!)
        }
    })



    let dateArr = createdByDate[indexPath.row]
    let strDate = Settings.dateFormatter(dateArr)

    cell?.closingDateLabel .text = strDate
    }//end of dataosource.isEmpty else clause

    //Getting Image

    // Configure the cell...

    return cell!
}

回答1:


You need to verify how many objects are returned in the query. Is it really 1 as you expected, or 6? Set a breakpoint and find out at this line:

    let objects = query.findObjects()

Another code that could cause the bug would be:

    self.dataSourceAnyObject.append(objects!)

Remember, the table view datasource method could be called a number of times. If you append to this array, it could erroneously append a number of times.



来源:https://stackoverflow.com/questions/33190993/getting-total-posts-from-parse-using-swift2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!