【问题标题】:How to properly reload tableview after saving new data to Parse?将新数据保存到 Parse 后如何正确重新加载 tableview?
【发布时间】:2015-08-28 11:42:29
【问题描述】:

我更新了我的 swift 项目并错误地删除了我的应用程序中的一个功能。我的 tableview 在上传到 Parse 后不再立即重新加载数据。只有当我关闭我的应用并重新打开它时它才会重新加载。

之前,在我上传数据之后,它会在执行 segue 后立即重新加载。

这是我的tableView 代码:

 override func viewDidLoad() {
        super.viewDidLoad()


        self.refreshControl = UIRefreshControl()
        refreshControl.addTarget(self, action: "refreshTable", forControlEvents: UIControlEvents.ValueChanged)
        self.homeTimelineTableView.addSubview(refreshControl)


        self.refreshControl.backgroundColor = UIColor.yellowColor()


        homeTimelineTableView.delegate = self;
        homeTimelineTableView.dataSource = self;



        var query = PFQuery(className: "Product")
        query.orderByDescending("createdAt")
        query.findObjectsInBackgroundWithBlock {
            (products: [AnyObject]?, error: NSError?) -> Void in

            if error == nil {
                // success fetching objects    
                for product in products! {

                    self.imagePNG.append(product["imagePNG"] as! PFFile)
                    self.shortDescription.append(product["shortDescription"] as! String)
                    self.productTitles.append(product["title"] as! String)
                    self.productPrice.append(product["price"] as! String)
                    self.productLongD.append(product["longDescription"] as! String)

                }

                // reload the timeline table
                self.homeTimelineTableView.reloadData()

            }else {

                println(error)
            }
        }

    }

不知道我错过了什么。我尝试在我的viewDidLoad 顶部添加self.homeTimelineTableview.reloadData(),假设这会告诉tableView 在控制器打开后重新加载,但这不起作用。

有什么建议吗?谢谢!

【问题讨论】:

  • 必须检查您的查询是否返回数据
  • 是的,正在返回现有数据。新数据,但是,我必须重新启动应用程序。

标签: swift uitableview parse-platform


【解决方案1】:

从主线程重新加载您的homeTimelineTableView

dispatch_async(dispatch_get_main_queue()) {
    self.homeTimelineTableView.reloadData()
}

【讨论】:

  • 是的,他是对的。任何 UI 更新都应该在主线程上执行。块在单独的线程上执行。根据经验,如果从块中调用任何 UI 更新,则应将其包含在 dispatch_async 中。
  • 我试过这个,但它似乎也不起作用。
  • 你确定你的tableview数组正在更新吗?
猜你喜欢
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多