UITableview reloading tableview and cell

非 Y 不嫁゛ 提交于 2019-12-12 04:42:38

问题


I am trying to reload my table view using

self.tableView.reloadData()

It works properly if I'm loading static datasource using array. Everything work properly. But when I try to use my query function with parse, it loads a new cell but the contents of the tableview cell doesn't change. If I re-open the app, the cells will update properly.

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

    //If datasource

    if dataSource.isEmpty{
        fetchDataFromParse()
        print("no posts")
    }

        let itemArr:PFObject = self.dataSource[indexPath.row]
        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



    return cell!
}

I am using pull to refresh my tableviews contents using this code

func refresh(sender:AnyObject)
{
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        self.fetchDataFromParse()
        self.tableView.reloadData()
        self.refreshControl?.endRefreshing()
    })

}

with or without the dispatch_asynch function the results remains the same. It just add new tableviewcell but the contents in it does not change. Any ideas guys?

edit 1 :

func fetchDataFromParse() {
    //        MARK: - JOB POST QUERY
    if PFUser.currentUser()?.objectId == nil{
        PFUser.currentUser()?.saveInBackgroundWithBlock({ (success, error) -> Void in
            let query = PFQuery(className: "JobPost")
            //creating a pointer
            let 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(self.dataSource)
                print(self.createdByDate)
            }
        })
    } else {
        let query = PFQuery(className: "JobPost")
        //creating a pointer
        let 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(self.dataSource)
            print(self.createdByDate)

        }
    }//end of PFUser objectID == nil else clause
}

回答1:


Let's see the content of the fetchDataFromParse() function where I presume you're filling the self.dataSource array




回答2:


Try to call self.tableview.reloadData() when fetchDataFromParse() is finished.




回答3:


Check whether your dataSource array is empty at the end of your fetchDataFromParse method




回答4:


PFUser.currentUser()?.saveInBackgroundWithBlock is an asynchronus method. So your tableView cell is having no data.



来源:https://stackoverflow.com/questions/33196646/uitableview-reloading-tableview-and-cell

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