table view cell deleting not working - ios

I've used the sdk standard code for deleting however it crashes once I press the delete button.
I am using this code
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[tableFavoritesData arrayWithObject:indexPath] withRowAnimation:YES];
}
}
I tried with both NSMutableArray instead of tableFavoritesData however nothing works.

Well, basically what you want to do is:
Delete the row from the data source (array).
Tell the table view that you have deleted a row from the data source.
Correct code should probably look like that:
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableFavoritesData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
EDIT: I didn't notice another error.
You need to specify the type of animation, not just pass YES or NO. For example: UITableViewRowAnimationFade. Check out possible UITableViewRowAnimation values here.
EDIT 2: For the comment below (comment formatting suck):
Check out NSNotificationCenter in the docs, especially addObserver:selector:name:object: and postNotificationName:object: methods.
In your other view controller (probably viewDidLoad method):
[[NSNotificationServer defaultCenter] addObserver:self selector:#selector(deletedRow:) name:#"RowDeleted" object:nil];
-(void) deletedRow:(NSNotification*) notification
{
NSDictionary* userInfo = [notification userInfo];
NSIndexPath indexPath = [userInfo objectForKey:#"IndexPath"];
// your code here
}
and while deleting the row:
if (editingStyle == UITableViewCellEditingStyleDelete) {
...
[[NSNotificationServer defaultCenter] postNotificationName:#"RowDeleted" object:self userInfo:[NSDictionary dictionaryWithObject:indexPath forKey:#"IndexPath"]];
}
Just remember that you need to remove observer from notification center when you dealloc the other UIViewController:
[[NSNotificationServer defaultCenter] removeObserver: self];
Hope I didn't make much errors, I don't have access to XCode atm.

If you looked at the console, it will most probably tell you that your model (your data structure) doesn't match what the table expects. I.e. your delegate method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
must return one less than before.
Also, [tableFavoritesData arrayWithObject:indexPath] looks very strange and is probably not what's expected. Maybe you want [NSArray arrayWithObject:indexPath] here. And remove the data from your model first.

Related

Unable to delete row/coredata

In my app I have a Table view with values from a mapkit. I can edit the table, delete the row, but when the app starts over, the same deleted rows are still there....How can I fix this? The code for the editing:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_locations removeObjectAtIndex:[indexPath row]];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView reloadData];
}
Any tips? Thank you
Load the data from core data using a NSFetchedResultsController.
Implement the delegate function to update your views when change occur.
When you delete something delete enqueue a block to delete it from core data. (if you are using NSPersistentContainer use performBackgroundTask:).
Don't update your views. This will happen automatically because of the delegate callback that you implemented in step 2.

TableView: How to proceed after catching 'NSInternalInconsistencyException'?

I have an application with a UITableView. The application communicates with a server.
Problem is the following:
Client 1 deletes a cell of the TableView. The data update is transmitted to the server which sends a data update to Client 2. In the same moment Client 2 deletes a cell from the TableView. Due to the receiving update an NSInternalInconsistencyException is thrown because the number of cells before and after are not as expected (difference is 2 not 1).
The app crashes in
[tableView endUpdates];
Of course I can catch the exception with
- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
#try{
// Send data update to the server
[sender sendDataToServer:data];
// Update tableview
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
#catch (NSException * e) {
// Hide red delete-cell-button
tableView.editing = NO;
// Jump to previous ViewController
[self.navigationController popViewControllerAnimated:YES];
}
}
}
But after the successful catch I get an
EXC_BAD_ACCESS exception in [_UITableViewUpdateSupport dealloc].
What can I do to prevent the app from crashing? What do I have to do in the catch-block to "clean" the situation? Reloading the tableview doesn't take any effect.
EDIT: numberOfRows-method looks like this:
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [dataSourceArray count];
}
#synchronized(lock) solved my problem.
I just had to make sure that only one thread is able to manipulate the data source array at the same time. Now everything works fine, no more 'NSInternalInconsistencyException'.
Thx to trojanfoe and luk2302.

ios delete row from table in ViewController

I try to manage to delete a row from an UITable which is part on an UIViewController. I use the Edit button in the navigation bar. Hitting it will put the table rows in edit mode. But when a delete button in a row is pressed I get an error ...'Invalid update: invalid number of rows in section 0…. when using the following:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:YES];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSMutableArray *work_array = [NSMutableArray arrayWithArray:self.inputValues];
[work_array removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
What do I miss here? The Apple documentation seems to be outdated somehow.
Thanks
The problem is simple. You are not updating your data model properly before deleting the row from the table.
All you do is create some new array and delete a row from that. That's pointless. You need to update the same array used by the other data source methods such as numberOfRowsInSection:.
The issue you are having is that you are not directly updating your table's data source. You first create a completely new array called work_array based on your data source (I'm assuming it's self.inputValues) and then you remove an item from it, and then try to delete a row, but your tableView's data source still contains the item you intended to remove.
All you need to do is ensure that self.inputValues is a mutable array and directly remove the object at the index for that array, like this:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.inputValues removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
I hope that helps!

How do I reference my data source to delete row from tableview using core data

I want to delete a row from my tableview. I cannot find a solution that works from the other simialr questions here.
I know I need to use the following code, But I cant seem to figure out how to properly finish this line of code to make it work.
[dailyIncomeArray removeObjectAtIndex:indexPath.row];
But what goes where is says "dailyIncomeArray"?
[what-goes-here removeObjectAtIndex:indexPath.row];
I have a getData method that populates my tableview. Next I want to be able to swipe to the left and hit that red delete button. Thanks in advance for your help!
Heres the rest of the method
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[dailyIncomeArray removeObjectAtIndex:indexPath.row];
[self.tableView reloadData];
//[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:results] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
Here's the error I'm receiving. I don't understand what this means.
No visible #interface for 'NSArray' declares the selector 'removeObjectAtIndex:'

App crashs with SIGABRT error when trying to delete a row in tableView

my app crashes when the user tries to delete a row from the UITableView and in the debugger I get the SIGABRT error.
Here is my code for deletion:
- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[[self displayedObjects] removeObjectAtIndex:[indexPath row]];
// Animate deletion
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[[self tableView] deleteRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationFade];
//[[self tableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
}
}
I got help in another question. And the problem is that the object would be deleted from the array but then before deleting the row from the table view , the numberOfRowsInSection is called again which then loads the array from disk again and give the number before the deletion due to a problem in my loading of the array. I put a break point at the numberOfRowsInSection and it is all clear now.
Does your [self displayedObjects]'s item count cover [indexPath row]? For example, your [self displayedObjects] only has 5 item but you want to removeObjectAtIndex:6.
I compared with my code to see where you went wrong.
//Removes From Table
[favorites removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
Favorites is my array. What I think you're going to need to do is change this line:
[[self displayedObjects] removeObjectAtIndex:[indexPath row]];
First, put that line immediately before this one:
[[self tableView] deleteRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationFade];
And then change self to the name of your array that the table is displaying.
ALSO! VERY IMPORTANT
Make sure the array that the table is displaying is a NSMutableArray NOT a NSArray
With an NSArray, you can't change (add or delete) values. However, with an NSMutableArray you can, so make sure you aren't displaying a NSArray
If I'm not mistaken, you're calling this code:
[[self tableView] deleteRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationFade];
from within a committed delete event. I believe all you need to do is update your backing store from the "tableView:commitEditingStyle:forRowAtIndexPath:" method. The delete has already been committed. You may also need refresh your table view afterwards also make sure to mark your table view as allowing updates.
Try commenting out all of this code and tell us what happens:
// Animate deletion
NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
[[self tableView] deleteRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationFade];

Resources