ios - Deleting causes app to crash -
i trying put first ios app (i'm php dev), , running issue. trying follow tutorial make simple task manager app, , have working except delete functionality.
as try delete item, app crashes following error:
2015-06-10 08:33:32.532 tasks[56594:1355112] *** assertion failure in -[uitableview _endcellanimationswithcontext:], /sourcecache/uikit_sim/uikit-3347.44/uitableview.m:1623 2015-06-10 08:33:32.538 tasks[56594:1355112] *** terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'invalid update: invalid number of rows in section 0. number of rows contained in existing section after update (1) must equal number of rows contained in section before update (1), plus or minus number of rows inserted or deleted section (0 inserted, 1 deleted) , plus or minus number of rows moved or out of section (0 moved in, 0 moved out).'
there's throw call stack message afterwards can post too, if need be.
here's masterviewcontroller.swift
document:
// // masterviewcontroller.swift // tasks // // created john doe on 6/9/15. // copyright (c) 2015 john doe. rights reserved. // import uikit class masterviewcontroller: uitableviewcontroller { var objects = [task]() var count: int { return objects.count } override func awakefromnib() { super.awakefromnib() } override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. self.navigationitem.leftbarbuttonitem = self.editbuttonitem() } override func viewdidappear(animated: bool) { super.viewdidappear(animated); self.objects = taskstore.sharedinstance.tasks; self.tableview.reloaddata(); } override func didreceivememorywarning() { // dispose of resources can recreated. super.didreceivememorywarning() } // mark: - segues override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { if segue.identifier == "showdetail" { if let indexpath = self.tableview.indexpathforselectedrow() { let task = taskstore.sharedinstance.get(indexpath.row) (segue.destinationviewcontroller as! detailviewcontroller).detailitem = task } } } // mark: - table view override func numberofsectionsintableview(tableview: uitableview) -> int { return 1 } override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return objects.count } override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell { let cell = tableview.dequeuereusablecellwithidentifier("cell", forindexpath: indexpath) as! uitableviewcell let task = taskstore.sharedinstance.get(indexpath.row) cell.textlabel?.text = task.title cell.detailtextlabel?.text = task.notes return cell } override func tableview(tableview: uitableview, caneditrowatindexpath indexpath: nsindexpath) -> bool { // return false if not want specified item editable. return true } override func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) { if editingstyle == .delete { taskstore.sharedinstance.removetaskatindex(indexpath.row) tableview.beginupdates() tableview.deleterowsatindexpaths([indexpath], withrowanimation: .fade) tableview.endupdates() } else if editingstyle == .insert { // create new instance of appropriate class, insert array, , add new row table view. } } }
a friend mentioned should add beginupdates() , endupdates() methods before , after code deletes, still same error message , crash.
any advice appreciated, feel reading through reference isn't getting me anywhere. thanks!
it has nothing beginupdates()
, endupdates()
; used if need multiple animations (insert x rows in section 1, remove y rows in section 2) @ once.
your problem lies in fact you're using local variable objects
, taskstore.sharedinstance
. deleting task taskstore, still using objects
determine amount of rows.
the easiest way rid of objects
entirely, , use taskstore.sharedinstance
. however, there cases makes sense retain local copy, e.g. if include 'save' , 'cancel' buttons. in case, should fetch objects in viewdidload
(as now), use objects
everywhere else, , when user clicks 'save', write changes store.
Comments
Post a Comment