调用tableView.reloadData()时出现“致命错误”,tableView已正确连接

时间:2017-12-04 04:23:09

标签: ios swift uitableview swift3 reloaddata

我正在尝试在程序运行时动态更新tableView。我相信我已经更新了正确加载数据的数组,但是当我按下调用self.eventTable.reloadData()的按钮时,我收到错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

以下是相关代码:

查看控制器:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

//Timer view
@IBOutlet weak var playButton: UIBarButtonItem!;
@IBOutlet weak var pauseButton: UIBarButtonItem!;
@IBOutlet weak var refreshButton: UIBarButtonItem!;
@IBOutlet weak var timerLabel: UILabel!

var counter = 0
var timer = Timer()
var isTimerRunning = false

//testing view container
var viewShowing = 1;

override func viewDidLoad() {

    // Do any additional setup after loading the view, typically from a nib.
    pauseButton.isEnabled = false

    hideAll();
    self.basicContainer.isUserInteractionEnabled = true;
    self.basicContainer.isHidden = false;

    self.timerLabel.text = String("00:00:00");

    eventTable.dataSource = self
    eventTable.delegate = self

    super.viewDidLoad()        
    loadEvents(event: "timer start")
}

...

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


// Add table to keep track of events
@IBOutlet weak var eventTable: UITableView!

var eventData = [Session]()

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellReuseIdentifier") as! eventTableViewCell
    let event = eventData[indexPath.row]
    cell.eventLabel.text = event.session
    return cell
}

private func loadEvents(event: String) {
    guard let event1 = Session(session: event) else {
        fatalError("Unable to instantiate event")
    }

    eventData += [event1]

    DispatchQueue.main.async() {
        self.eventTable.reloadData()
    }
}

func testPrint() {
    loadEvents(event: "testing cell adding")
    //self.eventTable.reloadData()
    viewWillAppear(false)
    print("This is a test print");
}

}

该函数在ViewDidLoad()中调用时工作正常,但在另一个类中的按钮调用它时(“这是一个测试打印”打印到控制台,所以我知道按钮调用正在通过)。

预期的行为是tableView (eventTable)重新加载,显示两个单元格,“计时器启动”和“测试单元格添加”(理想情况下,“测试单元格添加”位于顶部)。

还要强调eventTable连接到故事板,这似乎是这里的常见问题。

以下是Session.swift文件和eventTableViewCell.swift文件(如果有用的话):

Session.swift

import Foundation
import UIKit

class Session {

//MARK: Properties

var session: String

//MARK: Initialization

init?(session: String) {

    guard !session.isEmpty else {
        return nil
    }

    self.session = session
}

}

eventTableViewCell.swift

import Foundation
import UIKit

class eventTableViewCell: UITableViewCell {
//MARK: Properties
@IBOutlet weak var eventLabel: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    // Configure the view for the selected state
}

}

谢谢!

编辑:我调用testPrint()的ViewController。

import UIKit

class BasicViewController: UIViewController {

var VC = ViewController();


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

//Basic buttons
@IBOutlet weak var warmButton: UIButton!
@IBOutlet weak var dryButton: UIButton!
@IBOutlet weak var stimulateButton: UIButton!
@IBOutlet weak var controlButton: UIButton!
@IBOutlet weak var bedButton: UIButton!
@IBOutlet weak var tempButton: UIButton!
@IBOutlet weak var pulseButton: UIButton!
@IBOutlet weak var ecgButton: UIButton!
@IBOutlet weak var apgarButton: UIButton!
@IBOutlet weak var helpButton: UIButton!

//APGAR options
@IBOutlet weak var skinColor: UIButton!
@IBOutlet weak var pulse: UIButton!
@IBOutlet weak var grimace: UIButton!
@IBOutlet weak var flexion: UIButton!
@IBOutlet weak var respiratoryEffort: UIButton!



@IBAction func warmButton(sender: AnyObject) {
    VC.testPrint();
}   
}

看起来你可以说我正在实例化导致问题的新ViewController。我该怎么办呢?对Swift来说相当新鲜

1 个答案:

答案 0 :(得分:1)

我认为,你的问题出在以下几行代码中:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellReuseIdentifier") as! eventTableViewCell
    let event = eventData[indexPath.row]
    cell.eventLabel.text = event.session
    return cell
}
  • 您是否可以检查单元格标识符与 单元格标识符
  • 相同
  • eventData 数组
  • 中的行数