2015-06-30 12 views
-1

在我的应用程序中,每次运行应用程序时都会出现此错误。 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.storyboardNote that <code>Info</code>'s class is set to the class with tableView inside it.

非常感谢!

+0

试图改变自己的tableView的出口名称 – iAnurag

+0

我做到了,它仍然产生的零值误差。 @iAuurag – richbrz

+0

println(tableView)打印出来的内容是什么? – Tim

回答

0

我觉得你的代码是在这条线崩溃

cell.textLabel?.text = entityCellArray[indexPath.row] 

当你的entityCellArray是字符串数组所以下面添加符合这一 可以防止死机因与安全展开cell.textLabel?.text发生。

var x = entityCellArray[indexPath.row] as! String 
cell.textLabel?.text = x 

并确保您的数组不为空

+0

首先,一个简单但重要的质量规则是Swift编码,从来没有在Swift中强制转换或解包任何东西。其次,更新后的代码不会改变会导致崩溃的任何事情。第三,代码中唯一可能的崩溃是如果entityCellArray没有足够的indexPath.row元素,并且检查一个空数组是不完整的保护。 –

+0

错误的数组索引运行时错误不同于解包可选错误。 –