I am having a crash with deleting a cell from a tableview. I am using a NSMutableArray loaded from NSUserDefaults to load the cells into the tableview.
This is the crash:
Code:
2011-09-03 18:21:06.097 App[10356:707] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-1906/UITableView.m:1046
2011-09-03 18:21:06.100 App[10356:707] *** 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 (3) must be equal to the number of rows contained in that section before the update (3), 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).'
Why in the world are you using user defaults for this?
At any rate, you aren't removing anything from your array. The error message really is quite clear on this. It's one of the better ones.
I am using NSUserDefaults because my dataSource comes from another view controller.
I do realize that I am actually not deleting something from my nsmutablearray, I must have confused it for the 2nd line where I am deleting the row and actually nothing in the array.
And lastly, I realize and that end of the deletion code, I should do [mytableview reloadData];
I know your a promoter for properties :P, but I don't think that will work in this case, since I need this dataSource to be in memory when the app closes, opens, (all the time). So I don't think that will work.
Also I am now trying this code but with no luck as I am getting the same crash:
I am using NSUserDefaults because my dataSource comes from another view controller.
NSUserDefault is not a reasonable way to share something between viewControllers.
I mean, if your purpose is to permanent save and retrieve something, it is a good way to use it, but for sure not just for pass variables between classes....
Generally, the datasource must be updated before the tableView, and I guess for the first line in that method you meant to write removeObjectAtIndex rather than objectAtIndex. Apart from that the code should be fine, just remove the other instance of your new first line. Also, if you are storing the array in NSUserDefaults and then setting the cellArray property to the NSUserDefaults instance, you should really be reloading the tables datasource afterwards?
Quote:
Originally Posted by Objective Zero
I know your a promoter for properties :P, but I don't think that will work in this case, since I need this dataSource to be in memory when the app closes, opens, (all the time). So I don't think that will work.
Also I am now trying this code but with no luck as I am getting the same crash:
Ok so let's put aside that I shouldn't be using NSUserDefaults for now. I tried both dany_dev's suggestion and iSDK's suggestion but neither seemed to rectify the issue, it is the same crash as from before.
This is now the code I am using:
This leaves me with 2 questions:
1. Should I update NSUserDefaults before I do the line [tableview deleteRowAtIndexPaths.... ?
2. Also are you recommending that I should use properties to share the data? And if so, what would be the logic behind that? Right now I use user defaults because it is easier logically for me. Are there disadvantages using that to properties?
When you share objects between other objects (in this case view controllers), they remain in memory so there's no extra overhead. What you are doing is committing them to permanent storage, then reading them from there into memory again which is quite an expensive process. What exactly are you doing, what is the scenario, and why do you feel that sharing through properties is not a goo option?
Not to mention, you already ARE using properties. You're just using properties AND using user defaults. How could that possibly be faster/easier than using properties alone?
Oh yeah, and if you would have a multi-section table, your technique as-is falls apart really quickly.
@baja_yu I'll explain how I am doing everything currently. So I have a game here. When they lose the game, a game over view is shown which generates a NSDictionary from information about the user that is playing. That NSDictionary is added to a NSMutableArray and then stored in NSUserDefaults. Then once I go back to the Main Menu and then go to my Statistics view, I load that NSMutableArray from NSUserDefaults in my numberOfRowsInSection delegate method. Then when I go to delete a cell, I delete the object from my array and then save it to NSUserDefaults.
So technically, AFAIK, if I do it the way I do it now, if the user gets the game over view and then does not go to the Statistics view before the app is terminated, wouldn't that just pretty much delete the info for that game instance?
That is why I am using NSUserDefaults, because it is the easiest way (I think), with my scenario here.
@BrianSlick I am using only a one section tableview so that doesn't matter.