gpt4 book ai didi

ios - "fatal error"调用tableView.reloadData()时,tableView已正确连接

转载 作者:行者123 更新时间:2023-11-28 15:11:02 24 4
gpt4 key购买 nike

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

fatal error: unexpectedly found nil while unwrapping an Optional value

相关代码如下:

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

}

该函数在 ViewDidLoad() 中调用时工作正常,但当它被另一个类中的按钮调用时(“This is a test print”打印到控制台,所以我知道按钮调用正在进行中)。

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

还要强调的是,eventTable 是连接到 Storyboard的,这似乎是这里的一个常见问题。

这里是 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 相当陌生

最佳答案

我认为,您的问题出在这行代码中:

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数组中的行数

关于ios - "fatal error"调用tableView.reloadData()时,tableView已正确连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47626466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com