Ios 视图控制器未刷新

Ios 视图控制器未刷新,ios,swift,xcode,firebase-realtime-database,tableview,Ios,Swift,Xcode,Firebase Realtime Database,Tableview,我有一个带有集合视图的视图控制器,当用户单击单元格时,应用程序会进入一个视图控制器,该视图控制器具有用户所有帖子的表视图 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "userprofile" { let vc = segue.destination as! UsersProfileViewController let po

我有一个带有集合视图的视图控制器,当用户单击单元格时,应用程序会进入一个视图控制器,该视图控制器具有用户所有帖子的表视图

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "userprofile" {
let vc = segue.destination as! UsersProfileViewController
     let postsForUser: [[String: Any]] = posts.filter {
                        guard let post = $0 as? [String : Any] else {
                            return false
                        }
                        return post["uid"] as? String == uid
                        } as! [[String : Any]]
                    print("postForUser = \(postsForUser)")
                    vc.posts = postsForUser //vc.posts should be [[String: Any]]
                }}
在UsersProfileViewController中,单元格填充在CellForRowAt中

 let post = self.posts![(indexPath?.row)!] as! [String: AnyObject]


        cell.Title.text = post["title"] as? String
        cell.Author.text = post["Author"] as? String
        cell.ISBN10.text = post["ISBN10"] as? String
        cell.ISBN13.text = post["ISBN13"] as? String
        cell.CoverType.text = post["CoverType"] as? String
        cell.Price.text = post["Price"] as? String
每次视图出现时,我都要重新加载表视图

  override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.isNavigationBarHidden = true
        let indexPath = self.selectedIndex

        let cell:ProfileTableViewCell? = TableView.cellForRow(at: indexPath! as IndexPath) as! ProfileTableViewCell        
        if(vcUserBook == true) // from EditPostViewController
        {


            let post = self.posts![(indexPath?.row)!] as! [String: AnyObject]

            cell?.Title.text = post["title"] as? String
            cell?.Author.text = post["Author"] as? String
            cell?.ISBN10.text = post["ISBN10"] as? String
            cell?.ISBN13.text = post["ISBN13"] as? String
            cell?.CoverType.text = post["CoverType"] as? String
            cell?.Price.text = post["Price"] as? String


        }

        }

但当我运行它时,当我点击采集单元时,应用程序崩溃。

你可以重新加载数据

覆盖函数视图将出现(u动画:Bool){self.navigationController?.isNavigationBarHidden=true

YourTableView.reloadData()}

您只需重新加载数据即可

覆盖函数视图将出现(u动画:Bool){self.navigationController?.isNavigationBarHidden=true

YourTableView.reloadData()}

您不能像在
视图中那样启动
单元格
。tableView尚未完成绘制,这就是为什么会出现错误

视图中的所有这部分都将显示
,需要在
单元格中完成

let indexPath = self.selectedIndex
let cell:ProfileTableViewCell? = TableView.cellForRow(at: indexPath! as IndexPath) as! ProfileTableViewCell        
if(vcUserBook == true) // from EditPostViewController {
    let post = self.posts![(indexPath?.row)!] as! [String: AnyObject]

    cell?.Title.text = post["title"] as? String
    cell?.Author.text = post["Author"] as? String
    cell?.ISBN10.text = post["ISBN10"] as? String
    cell?.ISBN13.text = post["ISBN13"] as? String
    cell?.CoverType.text = post["CoverType"] as? String
    cell?.Price.text = post["Price"] as? String
}

在您的
视图中将出现
时,您只需执行
TableView.reloadData()
操作,并处理
单元格中的单元格,如在
视图中将出现
时,您不能启动
单元格。tableView尚未完成绘制,这就是为什么会出现错误

视图中的所有这部分都将显示
,需要在
单元格中完成

let indexPath = self.selectedIndex
let cell:ProfileTableViewCell? = TableView.cellForRow(at: indexPath! as IndexPath) as! ProfileTableViewCell        
if(vcUserBook == true) // from EditPostViewController {
    let post = self.posts![(indexPath?.row)!] as! [String: AnyObject]

    cell?.Title.text = post["title"] as? String
    cell?.Author.text = post["Author"] as? String
    cell?.ISBN10.text = post["ISBN10"] as? String
    cell?.ISBN13.text = post["ISBN13"] as? String
    cell?.CoverType.text = post["CoverType"] as? String
    cell?.Price.text = post["Price"] as? String
}


在您的
视图中将出现
您应该只执行
TableView.reloadData()
并处理
cellForRowAt中的单元格

是否有错误消息?或者只是没有刷新表数据?在
let cell:ProfileTableViewCell?=cellForRow(at:indexPath!as indexPath)as!ProfileTableViewCell
表示意外发现零您是否尝试重新加载整个表?因为根据你的代码,你试图只重新加载一个单元格。运行我的代码的结果是什么,你看到一个干净的表还是什么?我想重新加载整个表。我看不到一个干净的表,因为根据我的代码,它在转到userprofileviewcontrollerI之前崩溃了,我明白这一点。我的意思是,因为它没有破坏你的应用程序,你能看到表格吗?有错误消息吗?或者只是没有刷新表数据?在
let cell:ProfileTableViewCell?=cellForRow(at:indexPath!as indexPath)as!ProfileTableViewCell
表示意外发现零您是否尝试重新加载整个表?因为根据你的代码,你试图只重新加载一个单元格。运行我的代码的结果是什么,你看到一个干净的表还是什么?我想重新加载整个表。我看不到一个干净的表,因为根据我的代码,它在转到userprofileviewcontrollerI之前崩溃了,我明白这一点。我的意思是,因为它不会破坏你的应用程序,你能看到表格吗?我这样做了,但表格视图不会在每次视图出现时都重新加载。例如,当我单击单元格中的按钮时,它允许我转到另一个控制器并编辑单元格,一旦编辑发送到firebase,它就会展开序列。但是,当我松开该段时,tableview不会更新。这就是为什么每次视图出现时,我都会尝试更新表视图。当您转到视图时,视图将始终被调用。在执行
tableView.reloadData()
之前,请确保已更新数据源(用于填充tableView的数据模型)。这很重要,所以获取所有需要的数据,然后重新加载表。当我在CellForRow中包含tableView.reloadData()时,它没有更新,但它会在
用户中反复打印表视图的详细信息。在执行
tableView.reloadData()之前,会出现
视图
确保用相关数据更新了self.posts
。否则,您将不会加载新数据。我这样做了,但表视图不会在每次出现视图时都重新加载。例如,当我单击单元格中的按钮时,它允许我转到另一个控制器并编辑单元格,一旦编辑发送到firebase,它就会展开序列。但是,当我松开该段时,tableview不会更新。这就是为什么每次视图出现时,我都会尝试更新表视图。当您转到视图时,视图将始终被调用。在执行
tableView.reloadData()
之前,请确保已更新数据源(用于填充tableView的数据模型)。这很重要,所以获取所有需要的数据,然后重新加载表。当我在CellForRow中包含tableView.reloadData()时,它没有更新,但它会在
用户中反复打印表视图的详细信息。在执行
tableView.reloadData()之前,会出现
视图
确保用相关数据更新了self.posts
。否则,您将无法加载新数据。