Obj-C iOS Delete UITableViewCell - objective-c

Im trying to delete a UITableViewCell from a table. I am having a problem because sometimes the cell that I want to delete has been scrolled off the screen and potentially recycled. I get the following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]
The delete method works perfectly if the deleted cell is visible in the tableview.
Does anyone know how to solve this?
Thanks

the best way to delete a cell is to delete first the item in your data source (NSmutableArray) and then delete the cell or reload your uitableview
[yourArray removeObjectAtIndex:yourIndex];
[yourTableView reloadData];

Surely you populate your UITableView with data, right? This data most likely comes from a container class like an NSArray or something like that, I presume. Merely delete the item of data from the NSArray and update the number of rows in section with the new number of items. Don't try to delete the UITableViewCell if it is off-screen.

Related

OSX NSTableView insertRowAtIndexes

i already checked Using NSTableView insertRowsAtIndexes solution but It does not solve my problem.
i want insert row in nstableview at particular index(add dynamically)
index Set is valid, still it causes Program crash
NSIndexSet *indexSet=[NSIndexSet indexSetWithIndex:i];
[myTableView insertRowsAtIndexes:indexSet withAnimation:NSTableViewAnimationEffectFade];
1)is any thing wrong in my code?
2)Is there any another way to add row at particular index(add dynamically)?
The code is correct but first you have to insert the model object in the data source array to keep model and view in sync.
I got My mistake..... problem In other Code so This code is fine
But I want to add some points About insertRowsAtIndexes: method
Hope it will helps to other people
1)Dont called reloadData() because you are adding particular number of rows so calling reloadData() will reload all data and it will causes crash
2) Calling this method multiple times within the same beginUpdates and endUpdates block is allowed, and changes are processed incrementally
3)Most Important thing is indexSet must be within range
if you are Enter valid indexSet then The numberOfRows in the table view is automatically increased by the count of indexes.
4)you can select animation according to your need
Sample Code :
[yourTableView beginUpdates];
NSIndexSet* theIndexSet = [NSIndexSet indexSetWithIndex:[self.yourTableContents count]-1];
[yourTableView insertRowsAtIndexes:theIndexSet withAnimation:NSTableViewAnimationEffectFade];
[yourTableView endUpdates];

Deleting rows manually from UITableView in Xcode 6

I have the following code written for my delete button to delete selected rows from the UITableView.
-(IBAction)deleteItems:(id)sender {
NSArray *selectedCells = [self.autoCompleteView indexPathsForSelectedRows];
NSMutableIndexSet *indicesToDelete = [[NSMutableIndexSet alloc] init];
for (NSIndexPath *indexPath in selectedCells) {
[indicesToDelete addIndex:indexPath.row];
}
//arrayFromPlist is NSMutableArray
[autoCompleteView beginUpdates];
[autoCompleteView deleteRowsAtIndexPaths:selectedCells withRowAnimation:UITableViewRowAnimationAutomatic];
[autoCompleteView endUpdates];
[selectedObjects removeObjectsAtIndexes:indicesToDelete];
[autoCompleteView reloadData];
[alertMsg deleteConfirmation:#"Do you want to delete these items?"];
}
Please check the image for my UITableView and the delete button. I have kept the Editing property of my UITableview to "Multiple Selection during Editing" in the storyboard.
I get the following error when i press the Delete button shown in the screen.
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 (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 2 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
Not sure what is my mistake in the delete button code.
One definite problem is that you are removing the cells before you have removed the corresponding data from the model. That's wrong. Always change the model first; then change the cells in such a way as to match the model.

Deleting A TableView Cell Populated By A Dictionary

So, deleting a table view cell is pretty straight forward. You remove the object from the array, and reload the table views data. Now... I have a table view that is populated from an NSMutableDictionary rather than array (we need to because we are parsing data and storing it). Does anyone know how to delete a cell via this method? I've tried simply calling [tableView reloadData]; which surprisingly worked, but obviously created an endless loop. I do not like endless loops. Does anyone have any suggestions? I couldn't find any relevant information in my research.
This is some relevant code in the cellForRowAtIndexPath delegate:
if (![frString isEqual:#"COOL STUFF"] && [subString isEqualToString:#"COOL"]) {
NSLog(#"A COOL is showing where it shouldn't, and it's %#", subString);
//remove all cells starting with COOL
[myDictionary removeObjectForKey:[self.dictName objectAtIndex:row]];
[tableView reloadData];
}
Also, I'd rather hide the cell instead of permanently removing the info from the dictionary. Is this possible?
There are a few things you're doing wrong :
You shouldn't remove the rows in the cellForRowAtIndexPath, because that's called when the tableview wants to render. So you remove a row, and then it renders again, calls cellForRowAtIndexPath:,...
Either delete the rows, or reload the data. deleteRowsAtIndexPaths: deletes sets of rows with animation, reloadData reloads the whole tableView, so it cancels the animation, and deleteRowsAtIndexPaths: becomes pointlesss
Ensure that you return the proper numberOfRowsInSection: did you remove the data from your dictionary ?

NSInternalInconsistencyException when trying to dynamically modify number of rows in UITableView

I am attempting to use insertRowsAtIndexPaths so I can insert a table row. I am getting this error:
*** 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 (0) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted).'
What I am trying to do is to dynamically modify the number of rows in section in my attempt at rectifying the problem:
NSArray *indexPathIndex = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:0], nil];
[myTableView beginUpdates];
//***** HERE I AM TRYING TO DYNAMICALLY INCREASE NUMBER OF ROWS
[myTableView numberOfRowsInSection:1];
////////////////////////////////////////////////////////////
[myTableView insertRowsAtIndexPaths:indexPathIndex withRowAnimation:UITableViewRowAnimationTop];
[myTableView endUpdates];
Initially, my number of rows in section is 0. I try to increase this number in my efforts to address the aforementioned error however the code [myTableView numberOfRowsInSection:1] seems to have no effect.
Could somebody please suggest how I can fix this problem?
Thank you,
Victor.
You should not be setting the number of rows yourself, the UITableView manages that on its own. Remove the line.
myTableView numberOfRowsInSection:1];
Take a look at the UITableViewDataSource protocol, particularly the tableView:numberOfRowsInSection method. This will allow you to dynamically set the number of rows in the section.
A tableView has a datasource that is generally an array, that you can use to fill the cells of the tableView.
- (UITableViewCell*)tableView cellForRowAtIndexPath..... {
//Dequeueing or alloc-init-ing a cell
SomeClass *someObject = [someArray objectAtIndex:indexPath.row]; // Something
// like this, where someArray becomes the source array.
...
}
Now in some other part of your code, if you want to dynamically change the contents of the tableView, change the someArray and then change the tableView.
[someArray insertObjectAtIndex:someIndex];
[myTableView insertRowsAtIndexPaths....]; //The indexPath is generally
// consistent with the someIndex.

NSTableView sort problem(with KVC)

I had a weird problem on NSTableView sort.
I created a simple app with a NSTableView which
has 4 columns. Then I used KVC to bind it to a
Array controller.
Then I added some data to the NSMutableArray.
Bulid&&Run the app and I can see the data inside
the table and by clicking the header the data can
be sorted correctlly.
Everything works fine up till now.
Then I tried to add the "caseInsensitiveCompare" to
each columns. So I opened the IB, set the sort key and
selector ("caseInsensitiveCompare:") to each columns.
Then Bulid&&Run the app, but when I click the header to
sort, I got the error message:
-[NSCFNumber caseInsensitiveCompare:]: unrecognized selector sent to instance 0x1006254f0
-[NSCFNumber caseInsensitiveCompare:]: unrecognized selector sent to instance 0x1006254f0
......
Then I tried to delete all the content in the sort key and
selector of each columns. And the app became OK again.
What seems to be the problem? I am really confused...
ps:
If I use compare: instead of caseInsensitiveCompare:, everything works fine again...
One of the values (such as a property or key) of your objects in your NSMutableArray is of class NSNumber. Since this class deals with numbers, it doesn't respond to the caseInsensitiveCompare: selector. This selector is meaningful to NSString.
The column on your table view that is displaying number values should keep using compare: for sorting the values.