使用swift2从解析中获取总帖子 [英] getting total posts from parse using swift2

查看:87
本文介绍了使用swift2从解析中获取总帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我打开应用程序时,它都会返回6行,即使我用1条帖子创建了一个新用户,它也会返回6行.另外,当我拉动刷新数据时,保持不变,我必须再次重新打开应用程序才能看到添加的新数据.这是我的下面的代码,

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
}

这是内部单元格

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或6?设置断点并在此行查找:

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.

这篇关于使用swift2从解析中获取总帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆