How to give action to 'Delete' in editButtonItem? - iphone

I've in my tableview i've
self.navigationItem.rightBarButtonItem = self.editButtonItem;
clicking on the minus button shows Delete button. I want to know how to give an action to this Delete button.
Tableview shows a list of items from sqlite database.I'm using Xcode 4.2

The delete button is already linked to your table view, and tapping it will send tableView:commitEditingStyle:forRowAtIndexPath: to your table view's data source. You do your deletion here.
This method is normally included in the template for UITableViewController subclasses.

If you look in to the UITableViewDataSource Protocol Reference you will find in there tableView:commitEditingStyle:forRowAtIndexPath:.
Asks the data source to commit the insertion or deletion of a
specified row in the receiver.
In this method you will get the index path for the cell to delete. You have to remove the corespondent element in four data source. And you also need to remove the table view cell.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

When you press delete button it will automatically call this delegate method of UITableView:
- (void)tableView:(UITableView *)tableView commitEditingStyle:( UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath.
{
// Check if it's a delete button or edit button...
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Update the array and table view.
[eventsArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
// Commit the change.
NSError *error;
if (![managedObjectContext save:&error])
{
// Handle the error.
}
}
}

Implement tableView:commitEditingStyle:forRowAtIndexPath: in your table view data source (presumably your table view controller). See the docs on that method, or any of several Apple sample code projects (including what you get from the Xcode "master-detail app" template) for details & example usage.

I would add that you should make sure you are reloading the table data so it can update on the screen. Otherwise it will look like it didn't do anything.
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.myMutableArray removeObjectAtIndex:indexPath.row];
[self.tableView reloadData];
}

Related

All items of a UITableView disappear after deleting only one of them

Looks a trivial task, however I cannot find what I am doing wrong. I have a table view with sections and I am able to delete 1 item, but when I try to delete more than one item all items disappear from the table.
It is important to mention here that the scenario works well if I delete only 1 row. Deleting one row does not impact the items of the UITableView.
When I click the edit button, every row is candidate for deletion. Code is
[self.tableView setEditing:YES animated:YES];
When I am deleting a row the code calls:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *itemsOfGroup = [[table1 getItems] retain];
int section = indexPath.section;
section = section - 1;
MyItem *deleteItem = (MyItem*)[[[sections objectAtIndex:section] objectForKey:#"SectionEntries"] objectAtIndex:indexPath.row];
[itemsOfGroup removeObject:deleteItem];
[table1 setItems:[itemsOfGroup autorelease]];
[[[sections objectAtIndex:section] objectForKey:#"SectionEntries"] removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: indexPath] withRowAnimation:UITableViewRowAnimationRight];
[self createSections];
[self.tableView reloadData];
}
Is it possible to have a problem with my memory? (I think once the message "message sent to deallocated instance" appeared for an NSArray class)
Important to mention also that my items are not actually deleted from the data source. They only disappear from the table view.
Why are you autoreleasing itemsOfGroup when you've just set the Items in table 1 to it? Should be a release call after the set, or better to use the self... mechanism with properties. Check your memory allocations/deletions here. Are you throwing away all your content? Also, after your delete, what is your numberOfRowsInSection: and numberOfSections: returning? check those.

iOS - UITableView refresh table properly?

I have lots of data and lots of rows in my tableView.
when data changes I want to update my visible cells on the screen, I really don't want to use reloadData because it's an expensive call.
Is it possible to somehow update the visible cells only?
I tried calling : beginUpdate & endUpdate on the table, but that doesn't work all the time?
Any suggestions?
You can:
[tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows]
withRowAnimation:UITableViewRowAnimationNone];
For Swift 3:
tableView.reloadRows(at: tableView.indexPathsForVisibleRows!, with: .none)
Try calling 'deleteRowsAtIndexPaths', a method of UITableView, right after you delete the model from your data source. For example, if you are deleting a row in editing mode, you could write something like the code below. Note that the first line in the 'if' statement removes the object from the data source, and the second line cleans up the table:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ( editingStyle == UITableViewCellEditingStyleDelete ) {
[[[[[ItemsStore sharedStore] parameterArray] objectAtIndex:indexPath.section] objectForKey:#"data"] removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}

UITableView "swipe to delete" function freezes

I can't figure out why my tableView isn't updating after I tap the delete button.
Once I click it, the table view "freezes". If I click another row, so that the tableview goes to another level of the hierarchy and click back, I can see that the item has been deleted and everything works fine.
Here is my code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//[tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Do whatever data deletion you need to do...
[tableView beginUpdates];
NSManagedObject *obj = (NSManagedObject *)[entityArray objectAtIndex:indexPath.row];
[managedObjectContext deleteObject:obj];
NSError *error;
[managedObjectContext save:&error];
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES];
[self viewWillAppear:YES];
}
//[tableView endUpdates];
}
Any input on this problem would be appreciated.
Thanks.
Found same issue and stumbled upon this thread. But the reason for the tableview freeze issue was different in our case.
For the sake of posterity:
The UITableViewCell which goes into Edit mode to display the "insert" or "delete" buttons should never have its userInteractionEnabled property set to "NO".
By correcting this, the same tableview freezing issue was fixed for us.
I can't see a call to [tableView endUpdates] matching the [tableView beginUpdates] that is at start of the if.
Could it be for this reason that your table freezes?

UITableview editing: done button does nothing

My table is populated with data from a sql DB. When I click on edit I can delete rows and the entry in the DB, but when I click on Done nothing happens.
To implement deleting I use commitEditingStyle:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//Get the object to delete from the array
buddies *buddyObj = [appDelegate.buddiesArray objectAtIndex:indexPath.row];
[appDelegate removeBuddy:buddyObj];
//Delete the object from the table.
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView reloadData];
}
}
I have another table in this app and Edit/Done works fine.
The table with editing working is the first table showed when app starts, the non working table is called clicking on second item of a tabbar.
Any help is appreciated.
Thanks,
Max
Check that delegate is set properly.

How to delete cells from a table view?

I have an Edit button in my navigation bar, and I have an table view.
My edit button calls an -editAction method.
And then, I have this piece of code to delete a cell, but I don't know how I can make the edit button to call this code...or how the edit button can let the table view display those red delete circles for every cell, which then trigger this:
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the managed object at the given index path
NSManagedObject *eventToDelete = [eventsArray objectAtIndex:indexPath.row];
[managedObjectContext deleteObject:eventToDelete];
// Update Event objects array and table view
[eventsArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
// Commit the change
NSError *error;
if (![managedObjectContext save:&error]) {
// Handle the error
}
}
}
According to the UITableView class reference docs, the code:
tableView.editing = YES;
Should put the table into editing mode and display those red delete circles. Then when the user deletes a cell your data source method should be called.
You can also use [tableView setEditing:YES animated:YES]; for an animated effect
In your view controller's -viewDidLoad method add the Edit button:
self.navigationItem.rightBarButtonItem = self.editButtonItem;
This button will toggle the controller's editing mode, sending it -setEditing:animated: