我的應用程序的更新現在已準備就緒,但是我注意到直接從應用程序商店中的應用程序版本更新到我正在Xcode中處理的新版本似乎崩潰我的應用程序。在更新中添加新陣列的問題
基本上,我補充說,這是不存在於以前的版本,這是存儲在NSUserDefaults的筆記存儲數據的新陣列(不理想,我知道,但我寧願保持這種方式現在)
當我進入我的程序的表格視圖選項卡,它崩潰這一行:
cell.notesLabel.text = (notes.objectAtIndex(indexPath.row)) as? String
錯誤只是指出 - 「主題1:1.1斷點」,一旦表視圖選項卡被竊聽。
我正在和別人討論這個問題,他們建議我需要檢查數組是否存在於默認值中,如果缺失則創建並同步數組。
我不完全知道我怎麼會去,但我試圖從這個改變我的代碼:
if var tempNotes: NSArray = NSUserDefaults.standardUserDefaults().arrayForKey("notes") {
notes = tempNotes.mutableCopy() as NSMutableArray
}
要這樣:
if NSUserDefaults.standardUserDefaults().arrayForKey("notes") != nil {
var tempNotes: NSArray = NSUserDefaults.standardUserDefaults().arrayForKey("notes")!
notes = tempNotes.mutableCopy() as NSMutableArray
}
但它似乎沒有有所作爲。順便說一句,該代碼在viewWillAppear()中。
任何想法?
編輯:
didSelectRowAtIndexPath()
和numberOfRowsInSection()
代碼:
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
if tableView == self.searchDisplayController?.searchResultsTableView {
return searchResults.count
} else {
if names.count == 0 {
if enterButtonTapped == false {
backgroundLabel.text = "Before you add any transactions, you must first set a budget. You can do this by tapping the 'Budget' tab."
} else {
backgroundLabel.text = "You haven't added any transactions yet. Tap the add button to add a new transaction."
}
backgroundLabel.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
backgroundLabel.numberOfLines = 0
backgroundLabel.textAlignment = NSTextAlignment.Center
backgroundLabel.sizeToFit()
backgroundLabel.hidden = false
backgroundLabel.font = UIFont(name: "Avenir", size: 17)
backgroundLabel.textColor = UIColor.lightGrayColor()
clearButton.enabled = false
self.tableView.backgroundView = backgroundLabel
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
self.tableView.scrollEnabled = false
return 0
} else {
backgroundLabel.hidden = true
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
self.tableView.scrollEnabled = true
clearButton.enabled = true
return names.count
}
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:CustomTransactionTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTransactionTableViewCell
var arrowX: CGFloat = 0
var amountX: CGFloat = 0
if height == 480 || height == 568 {
arrowX = 260
amountX = 152
} else if height == 667 {
arrowX = 315
amountX = 217
} else {
arrowX = 354
amountX = 217
}
cell.notesLabel.alpha = 0
cell.paymentNameLabel.frame.origin.x = 20
cell.dateLabel.frame.origin.x = 20
cell.costLabel.frame.origin.x = amountX
cell.creditArrowImage.frame.origin.x = arrowX
cell.paymentArrowImage.frame.origin.x = arrowX
cell.selectionStyle = UITableViewCellSelectionStyle.None
if tableView == self.searchDisplayController?.searchResultsTableView {
cell.paymentNameLabel.text = (searchResults.objectAtIndex(indexPath.row)) as? String
var indexValue = names.indexOfObject(searchResults.objectAtIndex(indexPath.row))
cell.costLabel.text = (values.objectAtIndex(indexValue)) as? String
cell.dateLabel.text = (dates.objectAtIndex(indexValue)) as? String
cell.notesLabel.text = (notes.objectAtIndex(indexValue)) as? String
if images.objectAtIndex(indexValue) as NSObject == 0 {
cell.paymentArrowImage.hidden = false
cell.creditArrowImage.hidden = true
} else if images.objectAtIndex(indexValue) as NSObject == 1 {
cell.creditArrowImage.hidden = false
cell.paymentArrowImage.hidden = true
}
if notes.objectAtIndex(indexValue) as NSString == "" {
cell.notesLabel.text = "No notes to display."
}
//This works, unless there are two payments with the same name. To revert to previous version, remove tableView if statement and just keep code below the else.
} else {
cell.paymentNameLabel.text = (names.objectAtIndex(indexPath.row)) as? String
cell.costLabel.text = (values.objectAtIndex(indexPath.row)) as? String
cell.dateLabel.text = (dates.objectAtIndex(indexPath.row)) as? String
cell.notesLabel.text = (notes.objectAtIndex(indexPath.row)) as? String
if images.objectAtIndex(indexPath.row) as NSObject == 0 {
cell.paymentArrowImage.hidden = false
cell.creditArrowImage.hidden = true
} else if images.objectAtIndex(indexPath.row) as NSObject == 1 {
cell.creditArrowImage.hidden = false
cell.paymentArrowImage.hidden = true
}
if notes.objectAtIndex(indexPath.row) as NSString == "" {
cell.notesLabel.text = "No notes to display."
}
}
return cell
}
請發佈數據源的numberOfRowsInSection。另外,設置一個異常斷點,以確保哪一行導致崩潰。 – danh 2014-10-31 23:32:42
我已經用numberOfRowsInSection和didSelectRowAtIndexPath更新了我的問題。我設置了一個異常斷點,這絕對是我所說的導致崩潰的線。 – user3746428 2014-10-31 23:44:07