views:

55

answers:

2

I've got a UITableViewController with a bunch of random items in it. I want to be able to add items to it, and delete items from it. I have a UITextField in the first section header, so you can type in a word/phrase and add it to the list.

You can delete words from the list without issue, however, if you add a word to the list, then try to delete it, the app ends and returns to the home screen; nothing appears in the console.

I'm not sure where the problem lies. Here's the addNewWord method I've been using:

- (void)addNewWord
{
    NSString *newWord = [textField text];
    [array insertObject:newWord atIndex:0];
    NSLog(@"%d", [array count]);
    [textField setText:@""];

    [[self tableView] reloadData];

}

And here is the -(void) tableView:commitEditingStyle:forRowAtIndexPath method

- (void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [array removeObjectAtIndex:[indexPath row]];
        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationRight];
        [tableView endUpdates];
    }
    NSLog(@"%d", [array count]);
}

That's all I've come up with as to where the problem might be, which is also why I'm stuck.

Any insight is greatly appreciated. Thank you very much!

-Edit: Added [tableView beginUpdates]; and [tableView endUpdates]; same results. Any other ideas?

-Edit2: When I added the [tableView beginUpdates]; and [tableView endUpdates];, I get an exception. Here's the console output:

2010-09-30 05:56:40.172 DictionaryWordsTest[2350:207] *** Assertion failure in 
-[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/
UIKit-1262.60.3/UITableView.m:920
2010-09-30 05:56:40.175 DictionaryWordsTest[2350:207] *** 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 (17) must be equal to the 
number of rows contained in that section before the update (17), 
plus or minus the number of rows inserted or deleted from that section 
(0 inserted, 1 deleted).'
*** Call stack at first throw:
(
0   CoreFoundation                      0x02486b99 __exceptionPreprocess + 185
1   libobjc.A.dylib                     0x025d640e objc_exception_throw + 47
2   CoreFoundation                      0x0243f238 +[NSException raise:format:arguments:] + 136
3   Foundation                          0x000b2e37 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4   UIKit                               0x00332d37 -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 8719
5   UIKit                               0x00322a01 -[UITableView endUpdates] + 42
6   DictionaryWordsTest                 0x00002a73 -[DictionaryViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 140
7   UIKit                               0x0031f96d -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] + 101
8   UIKit                               0x002b87f8 -[UIApplication sendAction:to:from:forEvent:] + 119
9   UIKit                               0x00343de0 -[UIControl sendAction:to:forEvent:] + 67
10  UIKit                               0x00346262 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
11  UIKit                               0x00344e0f -[UIControl touchesEnded:withEvent:] + 458
12  UIKit                               0x002dc3d0 -[UIWindow _sendTouchesForEvent:] + 567
13  UIKit                               0x002bdcb4 -[UIApplication sendEvent:] + 447
14  UIKit                               0x002c29bf _UIApplicationHandleEvent + 7672
15  GraphicsServices                    0x02d66822 PurpleEventCallback + 1550
16  CoreFoundation                      0x02467ff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
17  CoreFoundation                      0x023c8807 __CFRunLoopDoSource1 + 215
18  CoreFoundation                      0x023c5a93 __CFRunLoopRun + 979
19  CoreFoundation                      0x023c5350 CFRunLoopRunSpecific + 208
20  CoreFoundation                      0x023c5271 CFRunLoopRunInMode + 97
21  GraphicsServices                    0x02d6500c GSEventRunModal + 217
22  GraphicsServices                    0x02d650d1 GSEventRun + 115
23  UIKit                               0x002c6af2 UIApplicationMain + 1160
24  DictionaryWordsTest                 0x00001f58 main + 102
25  DictionaryWordsTest                 0x00001ee9 start + 53
)
terminate called after throwing an instance of 'NSException'

Many Thanks!

-YuKagi

+1  A: 

You need to wrap your -[tableView deleteRowsAtIndexPaths] call with [tableView beginUpdates] and [tableView endUpdates].

Ben Gottlieb
Thank you for answering! I tried, but the issue remains. Any other thoughts?
YuKagi
The error tells you the problem: your -tableView:numberOfRowsInSection: is not returning the proper value after you delete the item.
Ben Gottlieb
I appreciate your help, without the begin and endUpdates call, I wouldn't have the error. Here is my problem though; I've done something similar, and haven't had this issue. I can't see where the error is. It looks like it's in the delete method, but I wonder if it's in the add method instead. Thoughts? Again, thank you very much!!
YuKagi
A: 

Oddly enough, the answer ended up being something other than what I had expected. I'm not sure what the difference is, and why I was getting no console output, but here's what I ended up doing.

In addNewWord, instead of using reloadData, I used these methods to reload the tableView, and insert the word at the top (So I didn't have to scroll to the bottom of the view to see the new word):

- (void)addNewWord
{
[[self tableView] beginUpdates];
NSString *newWord = [textField text];

[array insertObject:newWord atIndex:0];
[textField setText:@""];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[[self tableView] endUpdates];
}

Then, I think the key is, that addNewWord isn't called from:

[textField addTarget:self action:@selector(addNewWord) forControlEvents:UIControlEventEditingDidEndOnExit]; 

But, I added a button next to my textfield that calls it. Everything works now.

Thank you all for your help! I hope this helps someone in the future!

-YuKagi

YuKagi