在我的应用程序中,每次运行应用程序时都会出现此错误。 fatal error: unexpectedly found nil while unwrapping an Optional value
。这是非常令人沮丧的。我的类代码:我的UITableView不存在Swift
import UIKit
import Foundation
class MainViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var amountField: UITextField!
@IBOutlet weak var whoPaysLabel: UILabel!
@IBOutlet weak var statusLabel: UILabel!
@IBOutlet weak var datePicker: UIDatePicker!
var names:[String] = []
var amounts:[Int] = []
var dates:[String] = []
var whoPays:[String] = []
var status:[String] = []
var arrayNumber:Int = -1
@IBOutlet var tableView: UITableView!
let textCellIdentifier = "EntityCell"
var entityCellArray:[String] = []
let newDate = NSDate()
override func viewDidLoad() {
super.viewDidLoad()
println(tableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewWillAppear(animated: Bool) {
self.tableView.reloadData()
}
@IBAction func changeWhoPays(sender: UISwitch) {
if(sender.on){
whoPaysLabel.text = "\(nameField.text) pays you"
}else{
whoPaysLabel.text = "you pay \(nameField.text)"
}
}
@IBAction func changeStatus(sender: UISwitch) {
if sender.on{
statusLabel.text = "paid"
}else{
statusLabel.text = "not paid"
}
}
func createNewEntity(){
names.append(nameField.text)
var newAmount = self.amountField.text.toInt() ?? 0
amounts.append(newAmount)
let formatter = NSDateFormatter()
formatter.dateStyle = NSDateFormatterStyle.MediumStyle
var dateString = formatter.stringFromDate(datePicker.date)
dates.append(dateString)
if whoPaysLabel.text == "\(nameField.text) pays you" || whoPaysLabel.text == "... pays you"{
whoPays.append("they pay")
}else{
whoPays.append("you pay")
}
status.append(statusLabel.text!)
arrayNumber++
entityCellArray.append("\(names[arrayNumber]) - \(amounts[arrayNumber]) - \(dates[arrayNumber]) - \(status[arrayNumber])")
let created = UIAlertController(title: "success", message: "creation successful", preferredStyle: UIAlertControllerStyle.Alert)
created.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel, handler: nil))
presentViewController(created, animated: true, completion: nil)
nameField.text = ""
amountField.text = ""
whoPaysLabel.text = "..."
datePicker.date = newDate
println("\(entityCellArray[arrayNumber])")
printCount()
}
//test function, not to be implemented in app
func printCount(){
println(entityCellArray.count)
}
@IBAction func createButtonPressed(sender: UIButton){
createNewEntity()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return entityCellArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = entityCellArray[indexPath.row]
return cell
}
}
注:当我硬编码我entityCellArray
字符串值,并采取了self.tableView.reloadData()
,它工作得很好,显示所有的字符串像它应该。这是我的tableView设置的屏幕截图,其中包含Main.storyboard
。
非常感谢!
试图改变自己的tableView的出口名称 – iAnurag
我做到了,它仍然产生的零值误差。 @iAuurag – richbrz
println(tableView)打印出来的内容是什么? – Tim