我是应用程序开发的新手,我使用XCode和Swift创建了一个非常简单的待办事项列表应用程序。但由于某种原因,当用户删除任务,再次使用相同的名称创建它,然后删除该单元格时,iOS模拟器会因异常代码而崩溃:
Thread 1: EXC_BAD_ACCESS (code=EXC_i386_GPFLT)
这是回溯:
* thread #1: tid = 0xb80ca, 0x000000010a4ff00b libobjc.A.dylib`objc_msgSend + 11, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
frame #0: 0x000000010a4ff00b libobjc.A.dylib`objc_msgSend + 11
frame #1: 0x00000001092318be UIKit`-[UIApplication sendAction:to:from:forEvent:] + 75
frame #2: 0x0000000109338410 UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 467
frame #3: 0x00000001093377df UIKit`-[UIControl touchesEnded:withEvent:] + 522
frame #4: 0x0000000109277308 UIKit`-[UIWindow _sendTouchesForEvent:] + 735
frame #5: 0x0000000109277c33 UIKit`-[UIWindow sendEvent:] + 683
frame #6: 0x00000001092449b1 UIKit`-[UIApplication sendEvent:] + 246
frame #7: 0x0000000109251a7d UIKit`_UIApplicationHandleEventFromQueueEvent + 17370
frame #8: 0x000000010922d103 UIKit`_UIApplicationHandleEventQueue + 1961
frame #9: 0x00000001088da551 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
frame #10: 0x00000001088d041d CoreFoundation`__CFRunLoopDoSources0 + 269
frame #11: 0x00000001088cfa54 CoreFoundation`__CFRunLoopRun + 868
frame #12: 0x00000001088cf486 CoreFoundation`CFRunLoopRunSpecific + 470
frame #13: 0x000000010ca9b9f0 GraphicsServices`GSEventRunModal + 161
frame #14: 0x0000000109230420 UIKit`UIApplicationMain + 1282
* frame #15: 0x00000001087bb0fe swift course 013 todo list`top_level_code + 78 at AppDelegate.swift:12
frame #16: 0x00000001087bb13a swift course 013 todo list`main + 42 at AppDelegate.swift:0
frame #17: 0x000000010acc2145 libdyld.dylib`start + 1
相关代码:
@IBOutlet weak var toDoListTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if NSUserDefaults.standardUserDefaults().objectForKey("toDoList") != nil {
toDoList = NSUserDefaults.standardUserDefaults().objectForKey("toDoList") as [String]
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return toDoList.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel?.text = toDoList[indexPath.row]
return cell
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
toDoList.removeAtIndex(indexPath.row)
NSUserDefaults.standardUserDefaults().setObject(toDoList, forKey: "toDoList")
toDoListTable.reloadData()
}
}
override func viewDidAppear(animated: Bool) {
toDoListTable.reloadData()
}
我做错了什么?
答案 0 :(得分:0)
我不确定,但我认为您应该尝试在UIViewControler.swift中实现UITableViewDataSource!它对我有用。例如:
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
if editingStyle == UITableViewCellEditingStyle.Delete{
//Array.removeAtIndex(indexPath.row)
NSUserDefaults.standardUserDefaults().setObject(tdArr, forKey: "tdArr")
tableView.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return //Array.count
}