重新加载表数据时出现致命错误:展开为零:需要帮助
Posted
技术标签:
【中文标题】重新加载表数据时出现致命错误:展开为零:需要帮助【英文标题】:fatal error when reloading table data: unwraps to nil: need assistance 【发布时间】:2015-07-11 04:20:13 【问题描述】:我正在开发的应用程序有一个表格视图。似乎我在打印表格视图单元格值时犯了一个错误,因为每当我在应用程序运行时重新加载表格数据时,我都会收到一个致命错误,在展开时意外发现 nil。发生这种情况是因为表代码中有错误。帮助将不胜感激,谢谢。 完整代码: 导入 UIKit
class ViewController: UIViewController, UITableViewDelegate
var arr = [Int]()
var timer = NSTimer()
var countdowntimer = NSTimer()
var count = 0
var countdown = Int(arc4random_uniform(4000) + 1000)
var highscore:Int!
var buttonTapCounter = 0
let dateFormatter = NSDateFormatter()
@IBOutlet weak var beginButton: UIButton!
@IBOutlet weak var table: UITableView!
@IBOutlet weak var resetDataButton: UIButton!
@IBOutlet weak var averageLabel: UILabel!
@IBOutlet weak var bestLabel: UILabel!
@IBOutlet weak var startButton: UILabel!
@IBOutlet weak var highScoreButton: UILabel!
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var backgroundImage: UIImageView!
func updateTime()
countdown = countdown - 1
if countdown <= 0
startButton.textColor = UIColor.blackColor()
startButton.text = "Tap Now!"
countdowntimer.invalidate()
backgroundImage.image = UIImage(named: "red")
count = 0
Timer()
func Timer()
timer = NSTimer.scheduledTimerWithTimeInterval(0.001, target:self, selector:Selector("reflexTest"), userInfo:nil, repeats: true)
func reflexTest()
count = count + 1
timerLabel.text = "\(count) ms"
@IBAction func returnPressed(sender: AnyObject)
buttonTapCounter = 0
@IBAction func scoreButtonTouched(sender: AnyObject)
buttonTapCounter = 0
table.reloadData()
//THE table.reloadData() UP HERE IS CAUSING THE PROBLEM.
//I AM TYPING
//LIKE THIS SO YOU WILL
//NOTICE AS YOU SCROLL
//SORRY
//I AM TYPING
//LIKE THIS SO YOU WILL
//NOTICE AS YOU SCROLL
//SORRY
@IBAction func beginTapped(sender: AnyObject)
if startButton.text == "Tap when the Color Changes"
startButton.text = "You tapped too early!"
countdowntimer.invalidate()
countdown = Int(arc4random_uniform(4000) + 1000)
else
if count == 0
countdowntimer = NSTimer.scheduledTimerWithTimeInterval(0.001, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
startButton.text = "Tap when the Color Changes"
countdown = Int(arc4random_uniform(4000) + 1000)
timerLabel.text = "\(count) ms"
else
timer.invalidate()
backgroundImage.image = UIImage(named: "green")
startButton.text = "Tap to Begin"
arr.append(count)
count = 0
highscore = minElement(arr)
highScoreButton.text = "Best: \(highscore)"
saveDefaults()
func loadDefaults()
if let savedArray = NSUserDefaults.standardUserDefaults().objectForKey("data") as? [Int]
self.arr = savedArray
func saveDefaults()
NSUserDefaults.standardUserDefaults().setObject(self.arr, forKey: "data")
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return arr.count
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
dateFormatter.dateFormat = "MM/dd/yy HH:mm"
let dateInFormat = dateFormatter.stringFromDate(NSDate())
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
var tableArray:[Int]! = arr
cell.textLabel?.text = String(tableArray[indexPath.row])
return cell
override func viewDidLoad()
super.viewDidLoad()
loadDefaults()
highscore = minElement(arr)
if buttonTapCounter == 0
highScoreButton.text = "Best: \(highscore)"
// Do any additional setup after loading the view, typically from a nib.
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
需要修复的代码大概就是这个块:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
dateFormatter.dateFormat = "MM/dd/yy HH:mm"
let dateInFormat = dateFormatter.stringFromDate(NSDate())
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
var tableArray:[Int]! = arr
cell.textLabel?.text = String(tableArray[indexPath.row])
return cell
再次感谢大家,请记住:展开可选值时发现的错误 nil 与表格有关。谢谢!
【问题讨论】:
不。不起作用:(。问题出在 String(tableArray[indexPath.row]) 的某个地方 ..它在那里展开时发现了一个错误。这意味着我很可能必须在某处添加一个解释标记,我不知道在哪里,我遇到了麻烦。 记录您的 tableArray 并检查它是否为零.... 这样做的目的是什么:var tableArray:[Int]! = arr ? 【参考方案1】:我认为问题出在您的数组(arr)上。在控制台中打印 arr。 我通过采用静态整数数组尝试了您的代码,它工作正常。
【讨论】:
以上是关于重新加载表数据时出现致命错误:展开为零:需要帮助的主要内容,如果未能解决你的问题,请参考以下文章
SQLSRV 致命错误:翻译 Unicode 时出现严重错误