【发布时间】:2018-08-21 17:58:21
【问题描述】:
这里的问题是我在CommentVC 的当前上下文中以模态方式呈现EditCommentVC,因为我想将UIView 的背景设置为半透明。现在,在EditCommentVC 上,我有一个UITextView 允许用户编辑他们的评论,还有2 个按钮-cancel(关闭EditCommentVC)和update 更新新评论并将其推送到数据库。
在代码方面,一切正常,除了一旦新评论被推送并且EditCommentVC 被关闭,CommentsVC 上的UITableView 和所有 cmets 都不会重新加载以显示更新的 cmets .尝试从viewWillAppear() 调用它,但它不起作用。
在这种情况下如何重新加载UITableView 中的数据?
@IBAction func updateTapped(_ sender: UIButton) {
guard let id = commentId else { return }
Api.Comment.updateComment(forCommentId: id, updatedComment: editTextView.text!, onSuccess: {
DispatchQueue.main.async {
let commentVC = CommentVC()
commentVC.tableView.reloadData()
self.dismiss(animated: true, completion: nil)
}
}, onError: { error in
SVProgressHUD.showError(withStatus: error)
})
}
CommentVC 中的代码在其中转换(并传递注释的id)。 CommentVC 符合 CommentActionProtocol 传递该评论的 id:
extension CommentVC: CommentActionProtocol {
func presentActionSheet(for commentId: String) {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
let editAction = UIAlertAction(title: "Edit", style: .default) { _ in
self.performSegue(withIdentifier: "CommentVCToEditComment", sender: commentId)
}
actionSheet.addAction(editAction)
present(actionSheet, animated: true, completion: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "CommentVCToEditComment" {
let editCommentVC = segue.destination as! EditCommentVC
let commentId = sender as! String
editCommentVC.commentId = commentId
}
}
}
【问题讨论】:
-
Api.Comment.updateComment的回调是在后台线程中调用的吗?如果是这样,您可以将对reloadData的调用封装在DispatchQueue.main.async块中。 -
这是一个
async电话,是的。关于DispatchQueue.main.async,我应该在viewWillAppear()中这样做吗?因为我试过了,还是没有效果。 -
不,
viewWillAppear会在主线程上自动调用,无需在那里调度。我的意思是调度你在updateComment的回调块中的代码,包括对reloadData的注释掉调用,那是崩溃的。您还应该从主线程调用dismiss。 -
我已经更新了 OP 中的代码,但仍然崩溃。错误状态
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value -
@Dani 你需要展示你的主视图控制器代码,尤其是你展示这个视图控制器的地方。
标签: ios swift uitableview uiviewcontroller