我正在建立一个应用程序,这是一种学校的待办事项列表。我创建了一个名为DataService的类,它为我管理数据,这里是代码:
import Foundation
import UIKit
class DataService {
static let instance = DataService()
let KEY_ATIVS = "atividades"
let app = UIApplication.sharedApplication()
private var _loadedAtivs = [Atividade]()
var loadedAtivs: [Atividade] {
return _loadedAtivs
}
func saveAtivs() {
let ativsData = NSKeyedArchiver.archivedDataWithRootObject(_loadedAtivs)
NSUserDefaults.standardUserDefaults().setObject(ativsData, forKey: KEY_ATIVS)
NSUserDefaults.standardUserDefaults().synchronize()
}
func loadAtivs() {
if let ativsData = NSUserDefaults.standardUserDefaults().objectForKey(KEY_ATIVS) as? NSData {
if let atividades = NSKeyedUnarchiver.unarchiveObjectWithData(ativsData) as? [Atividade] {
_loadedAtivs = atividades
}
}
NSNotificationCenter.defaultCenter().postNotification(NSNotification(name: "atividadesLoaded", object: nil))
}
func addAtiv(ativ: Atividade) {
_loadedAtivs.append(ativ)
saveAtivs()
loadAtivs()
}
func scheduleNotification(text: String, fireDate: NSDate) {
let notification = UILocalNotification()
notification.fireDate = fireDate
notification.alertTitle = "Lembrete de atividade"
notification.alertBody = text
notification.soundName = UILocalNotificationDefaultSoundName
app.scheduleLocalNotification(notification)
}
func removeAtiv(index: NSIndexPath) {
_loadedAtivs.removeAtIndex(index.row)
app.cancelLocalNotification(app.scheduledLocalNotifications![index.row])
saveAtivs()
}
}
注意: Atividade 是一个自定义类
我使用表视图在单元格中显示数组,为此,我创建了一个自定义表视图单元类,它具有一个名为" configureCell"使用数组[indexPath.row]单独配置每个单元格。检查我所在的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("AtivCell") as? AtivCell {
cell.configureCell(DataService.instance.loadedAtivs[indexPath.row])
return cell
} else {
return AtivCell()
}
}
表视图工作正常,问题是在尝试从数组中删除元素时。我使用此代码执行此操作:
DataService.instance.removeAtiv(indexPath)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
DataService.instance.loadAtivs()
但它崩溃并给我以下错误:
2016-04-17 22:18:39.549 Homework[2430:249856] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3505.17/UITableView.m:1718
2016-04-17 22:18:39.550 Homework[2430:249856] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack:
(0x182154f5c 0x196d4bf80 0x182154e2c 0x183043f3c 0x18789d028 0x1878b77bc 0x10013f0dc 0x100106328 0x100106140 0x100106190 0x187afb180 0x187afb918 0x1879f3bf4 0x1879f6ddc 0x1877d7088 0x1876e580c 0x1876e5308 0x1876e5190 0x186eb5040 0x1009edd30 0x1009f3780 0x18210c258 0x18210a0c0 0x182038dc0 0x18d18c088 0x187712f44 0x10015d9f8 0x1975768b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
我不知道错误发生的原因。我从数组中删除元素,保存没有元素的数组,删除表视图中的行并再次将数组加载到configureCell函数,那么为什么崩溃发生以及如何解决?
请帮助我!
注意:元素在崩溃后从阵列中删除,但是我不希望用户体验崩溃只是为了删除阵列的某些元素