简体   繁体   中英

“fatal error” when calling tableView.reloadData(), the tableView is properly connected

I am trying to dynamically update a tableView while the program is running. I believe I have updated the array that the data loads from correctly, but when I press the button that calls self.eventTable.reloadData() I receive the error:

fatal error: unexpectedly found nil while unwrapping an Optional value

Here is the relevant code:

View Controller:

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");
}

}

The function works fine when it is called in ViewDidLoad() , but not when it is called by the button in another class ("This is a test print" prints to console so I know the button call is going through).

Expected behavior is the tableView (eventTable) reloading showing two cells, "timer start" and "testing cell adding" (ideally with "testing cell adding" being at the top).

Also want to emphasize that eventTable is connected to the storyboard, which seems to be a common problem on here.

Here is the Session.swift file and the eventTableViewCell.swift file if those are helpful:

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
}

}

Thanks!

Edit: The ViewController from where I call testPrint().

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();
}   
}

It would seem that you are all right in stating that I am instantiating a new ViewController which is causing the issue. How should I go about fixing this? Fairly new to Swift

I think, your problem is in this lines of codes:

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
}
  • Can you check the cell identifier is same as your cell identifier
  • And number of rows in eventData array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM