【问题标题】:UITableView.reloadData() is not refreshing tableView. No error messageUITableView.reloadData() 没有刷新 tableView。没有错误信息
【发布时间】:2018-07-21 23:40:13
【问题描述】:

我已经看过帖子UITableView.reloadData() is not working。我不确定它是否适用于我的情况,但如果我错了,请告诉我。

我的应用有一个 tableView。从主视图控制器中,我打开另一个视图控制器,创建一个新对象,然后将该对象传递回原始视图控制器,在那里它被添加到一个称为计时器的数组中。所有这些工作正常。但是,当我在 didUnwindFromNewTimerVC() 中调用 tableView.reloadData() 以显示计时器数组的更新内容时,没有任何反应。

注意:我已验证计时器数组已使用新对象进行了更新。它的计数增加,我可以访问它的成员。 didUnwindFromNewTimerVC() 中的其他所有内容都正常执行。 tableView 只是没有更新以反映它。

这是我的代码:

    import UIKit
    class TimerListScreen: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tabelView: UITableView!

    var timers = [Timer]()

    let tableView = UITableView()

    override func viewDidLoad() {
    super.viewDidLoad()

    tabelView.delegate = self
    tabelView.dataSource = self

    let tempTimer = Timer(timerLabel: "temp timer")
      timers.append(tempTimer)
    }

    @IBAction func didUnwindFromNewTimerVC(_sender:UIStoryboardSegue){
      guard let newTimerVC = _sender.source as? newTimerVC else{return}
      newTimerVC.timer.setTimerLabel(timerLabel: newTimerVC.timerLabel.text!)
      timers.append(newTimerVC.timer)
      tableView.reloadData()

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if let cell = tabelView.dequeueReusableCell(withIdentifier: "TimerCell", for: indexPath) as? TimerCell{

        let timer = timers[indexPath.row]

        cell.updateUI(Timer: timer)

        return cell

    }else{
        return UITableViewCell()
      }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return timers.count
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
       return 78
      }
    }

谢谢

【问题讨论】:

  • 在 viewWillAppear 中试试看??
  • 我尝试过以防万一,但效果相同。原来的代码块正在执行,但是对tableView没有可见的影响

标签: ios iphone uitableview swift4


【解决方案1】:

请注意拼写。有两个表格视图实例:出口 tabelView 和一个(无意义的)实例 tableView

重新加载outlet的数据

tabelView.reloadData()

并删除第二个实例let tableView ...的声明行。

但我建议将插座重命名为正确拼写的tableView(您可能需要在 Interface Builder 中重新连接插座)。


并强制解开单元格

let cell = tabelView.dequeueReusableCell(withIdentifier: "TimerCell", for: indexPath) as! TimerCell

并删除if - else 部分。如果一切都在 IB 中正确连接,则代码不得崩溃。

【讨论】:

  • 嗯,那很痛苦。感谢您指出了这一点!我盯着它看了好几个小时也没看到。
猜你喜欢
  • 2013-07-25
  • 2017-01-04
  • 1970-01-01
  • 2011-12-22
  • 1970-01-01
  • 1970-01-01
  • 2018-08-02
  • 1970-01-01
  • 2017-06-21
相关资源
最近更新 更多