0

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

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!
}
4

1 回答 1

1

您需要验证查询中返回了多少对象。真的是1,还是6?设置断点并在此行查找:

    let objects = query.findObjects()

另一个可能导致错误的代码是:

    self.dataSourceAnyObject.append(objects!)

请记住,表视图数据源方法可能会被多次调用。如果您追加到此数组,它可能会错误地追加多次。

于 2015-10-19T03:47:11.547 回答