【问题标题】:How to update data on swift immediately如何立即在 swift 上更新数据
【发布时间】:2015-11-10 22:07:00
【问题描述】:

我想在按下like 按钮后立即更新 swift 上的likeNum 数据。在下面的 tapLike 函数中,我更新了服务器端的 likeNum 数据(由 rails 编写),然后通过 self.tableView.reloadData() 重新加载数据。但是,它不能同时更新显示的数据。您能告诉我如何立即更新数据吗?

func tapLike(recognizer: UITapGestureRecognizer){
        var userId = self.defaults.objectForKey("uid") as! Int
        let parameters = [
            "id": recognizer.view!.tag,
            "user_id": userId
        ]

        Alamofire.request(.POST, uri.answersApi + "/like", parameters: parameters, encoding: .JSON)
            .responseJSON { (request, response, data, error) in
                self.tableView.reloadData()
        }
    }
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("qaIdentifier", forIndexPath: indexPath) as! QATableViewCell
    cell.likeNum.text = String(answers[indexPath.row].likeNum!)
    return cell
}

【问题讨论】:

  • 抱歉,我发现了另一个大问题。原因是我没有将数据从服务器端重新加载到客户端的模型中。

标签: swift uitableview reload alamofire


【解决方案1】:

UI 只能从main thread 更新:

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

【讨论】:

  • 谢谢Kheni!我试试看!
猜你喜欢
  • 2019-09-19
  • 1970-01-01
  • 2012-01-03
  • 2021-08-18
  • 2020-10-16
  • 1970-01-01
  • 1970-01-01
  • 2022-09-28
  • 1970-01-01
相关资源
最近更新 更多