【问题标题】:how to Reload data on table view without crashing in swift 3?如何在 swift 3 中重新加载表视图上的数据而不会崩溃?
【发布时间】:2017-06-20 05:45:18
【问题描述】:

我的应用程序中有一个 TableView,当用户点击其中一个单元格中的按钮时,safari 将在应用程序中打开,当 safari 关闭时应用程序将崩溃,因为我想更新 tableview 中的参数之一所以我需要从 Json 中删除所有并再次附加它们但这需要几秒钟,所以应用程序会崩溃,因为没有数据可以显示我使用了 Timer 但是 timer 不是一个好的解决方案,因为有时它会比 timer 花费更多的时间,所以它会再次崩溃我需要一个等待完成然后做其他事情的方法,如果你认为这也不是一个好的解决方案,请指导我哪个解决方案对这个问题有好处

这是我在应用程序中关闭 safari 后在我的应用程序中使用的内容(不是所有代码,但这些代码有问题)

 if let payed = dict.value(forKey: "payed"){

                            factorViewController.payed.removeAll()
                            self.factorTableView.reloadData()
                            factorViewController.payed.append(payed as! Int)
                            self.factorTableView.reloadData()

                            print("payed = \([payed])")

                        }

【问题讨论】:

  • 什么是崩溃报告?
  • 索引超出范围,因为当我删除 factorViewController.payed 中的数据时,没有信息显示
  • 添加 cellforrow 和 numberofrows 方法
  • 不,你没有完全阅读我的问题我很容易添加了这些方法,应用程序将在没有更新信息的情况下运行问题是我想在我的表格视图中更新信息
  • 我的意思是在你的问题中添加你的cellforrownumberofrows ,这样我就可以检查你做了什么。为什么要为重新加载 tableview 添加计时器??

标签: ios json uitableview swift3 wait


【解决方案1】:

尝试只重新加载一次数据:

 if let payed = dict.value(forKey: "payed") {
     // Empty Array here
     factorViewController.payed.removeAll()
     // You are appending here, so array will not be empty
     factorViewController.payed.append(payed as! Int)
     // Now you can reload data
     self.factorTableView.reloadData() 
     print("payed = \([payed])")
     // Also, if this doesn't work, you can use a callback(completion handler).
 }

这样,当你需要重新加载数据时,你的数组不会为空

【讨论】:

  • numberOFRows 返回array.count,现在我清空我的数组并重新加载tableview。你认为它会与IndexOutOfBound 一起崩溃吗??
  • 如果是这种情况,您需要使用回调。你能告诉我们你在哪里访问数组吗?
  • factorViewController.title.count 这将计算未付款的行数
  • 当你在同一个VC中时,永远不会崩溃并且不需要使用回调。如果计数为零,则不会调用行数,因此您的答案没有意义。
  • 但是这个答案会再次崩溃
【解决方案2】:

TableView 的工作原理

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return your_array.count // It returns how much cells you want in your tableview.
    }

 // This func defines what you wanted in your cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = dequeue_property // to dequeue your cell
     your_lbl.text = your_array[indexpath.row] // as per logic
    return cell
} 

你的情况

numberOfRowsInSection 中的数组计数多于您在cellForRowAt 中使用的数组,因此出列单元格的索引比您在cellForRow 中使用的数组多。这就是为什么 IndexOutOFRange 没有获得索引并崩溃的原因

你有数组问题而不是 tableView 重新加载问题

请参考: Swift array always give me "fatal error: Array index out of range"

【讨论】:

  • 我知道怎么用tableview了!我只是想在tableview中使用等待方法来获取信息
  • @SaeedRahmatolahi 你没明白我在说什么
  • 你的意思是我必须删除单元格中的所有数据,然后重新加载所有数据?
【解决方案3】:

好的,这比我想象的要容易!我刚刚在我的应用程序中定义了另一个变量,当开始获取 Json 时,将新数据附加到新变量,然后将新变量等于旧变量!就是这样!

【讨论】:

    猜你喜欢
    • 2021-07-18
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多