简体   繁体   English

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

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

I am trying to dynamically update a tableView while the program is running. 我正在尝试在程序运行时动态更新tableView 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: 我相信我已经正确更新了数据加载的数组,但是当我按下调用self.eventTable.reloadData()的按钮时,我收到了错误消息:

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). ViewDidLoad()调用该函数时,该函数工作正常,但在另一个类中的按钮调用该函数时,该函数则不能正常工作(“这是测试打印”会打印到控制台,因此我知道该按钮调用正在进行中)。

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). 预期的行为是tableView (eventTable)重新加载,其中显示了两个单元格:“计时器启动”和“测试单元格添加”(理想情况下,“测试单元格添加”在顶部)。

Also want to emphasize that eventTable is connected to the storyboard, which seems to be a common problem on here. 还想强调一下eventTable连接到情节提要,这似乎是这里的常见问题。

Here is the Session.swift file and the eventTableViewCell.swift file if those are helpful: 这是Session.swift文件和eventTableViewCell.swift文件(如果有帮助的话):

Session.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 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(). 编辑:我从哪里调用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();
}   
}

It would seem that you are all right in stating that I am instantiating a new ViewController which is causing the issue. 看来我可以实例化导致问题的新ViewController似乎还不错。 How should I go about fixing this? 我应该如何解决这个问题? Fairly new to Swift 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 以及eventData数组中的行数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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