UITableview重载tableview和单元格 [英] UITableview reloading tableview and cell

查看:182
本文介绍了UITableview重载tableview和单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用

self.tableView.reloadData()

如果我使用数组加载静态数据源,它将正常工作.一切正常. 但是,当我尝试将查询功能与解析一起使用时,它会加载一个新单元格,但tableview单元格的内容不会更改.如果我重新打开该应用程序,则单元格将正确更新.

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

我正在使用pull来使用此代码刷新我的tableviews内容

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()
    })

}

带有或不带有dispatch_asynch函数的结果保持不变.它只是添加了新的tableviewcell,但其中的内容没有改变.有想法吗?

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?

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
}

推荐答案

让我们看看fetchDataFromParse()函数的内容,我假设您正在填充self.dataSource数组

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

这篇关于UITableview重载tableview和单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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