2014-10-31 21 views
0

我的應用程序的更新現在已準備就緒,但是我注意到直接從應用程序商店中的應用程序版本更新到我正在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 
} 
+0

請發佈數據源的numberOfRowsInSection。另外,設置一個異常斷點,以確保哪一行導致崩潰。 – danh 2014-10-31 23:32:42

+0

我已經用numberOfRowsInSection和didSelectRowAtIndexPath更新了我的問題。我設置了一個異常斷點,這絕對是我所說的導致崩潰的線。 – user3746428 2014-10-31 23:44:07

回答

1

只是一個快速脫脂表明有複雜的邏輯決定多少行至numberOfRows返回,然後複雜--and different--邏輯根據cellForRow中的indexPath訪問數組。在我們甚至到達用戶默認問題之前 - 代碼是崩潰的祕訣。兩種方法的適當形式是把他們緊密對應起來:

  • 在numbeOfRows,決定你要回答的計數 爲其陣列。這是你的模型。
  • 在cellForRow中,取消引用與indexPath.row相同的數組 。你永遠不會有一個索引越界這樣 方式,因爲如果回答n爲numberOfRows(其中 N = someArray.count),然後cellForRow將只與行 0..N-1
稱爲

關於用戶默認值問題:您的新應用程序版本必須準備好處理您的舊版本所遺留的任何用戶默認值狀態,包括缺少用戶從未擁有舊版本的用戶。爲了防患於未然,請始終通過方法將數組默認設置爲默認值。它應該在默認情況下查找數組。如果找到,請將可變副本緩存爲實例變量並將其返回。否則,如果找不到(您發佈的測試代碼arrayForKey("notes") != nil與找不到的代碼相反),請將您的實例var初始化爲新數組並將其返回。