Use a button to call - (void)tableView:(UITableView *)tableView commitEditingStyle? - ios

I'd like to use a delete button to call the delete row function in a TableView cell, just like the iPhone message app works.
The code I use to trigger this from a swipe will be familiar:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[arrayLocations removeObjectAtIndex:indexPath.row];
[tableView reloadData];
}
But how do I call this code/functionality when pressing say a red circular delete button in the cell?

I'm not sure you want/should trigger this delegate yourself. Also remember that this delegate inserts / deletes cells so you need to check that editingStyle param. If you're adding a custom button you can simply route this to a custom method and delete the row.
Cell
Button action hit
Call a delegate
Table
Get the index with indexPathForCell
Adjust the data source array
Call begin updating, delete row, end updating

Related

How do i disable/enable edit button when user doesn't fully swipe to delete table view cell?

I have a tableView that allows deletion by swiping a cell or entering edit mode via an "Edit" button. The problem is, I'm not sure how to properly enable/disable edit mode; it should be disabled as soon as a user begins to swipe a row, and enabled after "unswiping" a row.
Currently, I disable in editingStyleForRowAtIndexPath (which returns delete) and enable in didEndEditingRowAtIndexPath.
The problem is, what if the user doesn't finish their swipe? For example, they begin to swipe a row, but release before fully swiping (causing the row to bounce back and hide the delete button). In this case, didEndEditingRowAtIndexPath does not get called. How do I ensure the table is editable after a half-swipe?
Here you are another approach that work properly,(quit or comment: editingStyleForRowAtIndexPath and didEndEditingRowAtIndexPath.). And you must implement only this:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Configure delete action:
// First remove the delete data from you data source.
Put here you code, like this:
[self.theMutableArrayWithDataSourde removeObjectAtIndex:indexPath.row];
// Second remove the cell:
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}

How to leave UITableViewCellEditingStyleDelete mode when user edit it with swipe gesture

I need to leave delete mode after user tapped Delete button. I want to show some activity indicator and wait the server response on delete action before I actually remove the cell (or not if the server does not respond). This action I want to perform from delegate method:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
How can I do it?
To hide the "Delete"-Button, you need to use the setEditing:animated: method.
However, your implementation of tableView:commitEditingStyle:forRowAtIndexPath: needs to execute this with a slight delay. The note in the reference Table View Programming Guide for iOS underneath Figure 7-1 states:
Note: The data source should not call setEditing:animated: from within its implementation of tableView:commitEditingStyle:forRowAtIndexPath:. If for some reason it must, it should invoke it after a delay by using the performSelector:withObject:afterDelay: method.
So your implementation could be like this:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// remove delete button only after short delay
[self performSelector:#selector(hideDeleteButton:) withObject:nil afterDelay:0.1];
}
}
- (void)hideDeleteButton:(id)obj
{
[self.tableView setEditing:NO animated:YES];
}
The above code makes the Delete-Button slide away after 0.1 seconds when the user presses it. You will then, of course, need to prevent it going back into the editing mode while waiting for the action to complete. For that, you can override the UITableViewDataSource method tableView:canEditRowAtIndexPath: to prevent the same cell or the whole table to be edited again while waiting.
In my code I make a call to
[self.tableView reloadData];
I'm not sure if this is the 100% correct way to do it but it works for me. So your function could be something like:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Do your processing.
[self.tableView reloadData];
}
It should then clear out the red Delete Button and refresh your data.
You may also be able to play around with the call
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
but in my experience its the reloadData call that seems to do the trick
Hi Let me get it straight: Your datasource is actually online and you want confirmation that it is deleted before you update your tableview - in the mean time you want to display an AI Activity indicator.
If that is what you want to do then you start with this method
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Get the key of the data you want to delete.
data_struct_instance * data = [self.mutableArray_data_source objectForRowAtIndexPath:indexPath];
NSString *data_key = data.key;
row_index = indexPath.row; // set a property to preserve the row (or index path for multilevel data) for use when you delete the the record later.
[self start_UIActivityIndicator];
[self callonline_with_delete_command:data_key]; // send the request to delete the record
}
once the server responds depending on whether it is successful or not you can either delete the record or reload the entire array if the table is small this is preferable to ensure that the data is synced -
....
[activityindicator StopAnimating];
if (success) {
[self.mutableArray_data_source removeObjectAtIndex:row_index];}
else {
NSLog ....
}
[self.tableView reloadData]; // resets the delete button.

Show another button next to Delete on UITableViewCell with a swipe

My app is build in a way that a user enters data inside a UITableView in UIViewcontroller A (one line of data or more) and can save it to disk. In the next UIViewController B he can see all the saved data.
I'm using this:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
}
}
To show a delete button on a cell with a swipe. How can I show an edit button on a single cell (that will appear the same way the delete button appears) next to delete button?
(the edit button will take the data back to the first UIViewController A for editing but that's not really relevant)
I thought about using a swipe gesture but got an error saying I can't use that on a prototype cell. I tried using it on the all UITableVIew but that conflicts with the swipe delete gesture.

If I have a reference to a UITableViewCell that has to be deleted, how can I delete it from an NSFetchedResultsController?

I've implemented a NSFetchedResultsController, and I've successfully implemented row deletion via the delete button revealed by a swipe (this is the standard control in UITableView, not something custom I've made). Ive done this by overwriting - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath so that the entity in the Core Data database that the cell references is deleted.
Now I want to implement my own deletion control. I've added a UIPanGesture to my UITableViewCell subclass so that when you pan a cell, a delete button is shown that moves in as the cell is swiped out (similar to that in iOS 7 mail app, or Clear app). Via a custom protocol, the view controller that has the NSFetchResultsController recives the cell whose custom delete button has been pressed. With this I can calculate the indexPath by doing NSIndexPath *indexPath = [self.tableView indexPathForCell:cellToDelete];
with the cell that needs to be deleted. Only problem is: How do I delete this cell?
UITableView has a deleteRowsAtIndexPaths: withRowAnimation: method. As far as I can tell, NSFetchResultsController does not have such a method. I guess what I need is somehow for - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath to be called since I've already implemented deletion in there.
Summary: In A UIViewController I have an NSFetchedResultsController and a pointer to a cell (as well as it's indexPath) that needs to be deleted. How do I delete the cell?
The FRC is driven by the contents of your Corw Data model, so, you need to delete the model object associated with the row and the FRC will pick up the change (it is observing the data store) and remove the cell from the table view.

iPhone delete messages like interface

I want to delete rows in a UITableView, I need to provide the delete button not by swipe but in a way similar to delete messages functionality in iPhones. In which on clicking edit a small red circle appears on the left in the cell which rotates when clicked and the delete button is shown. How to implement this?
Like in the image below:
See the Apple documentation here for details on how to achieve this. In particular:
When sent a setEditing:animated: message (with a first parameter of
YES), the table view enters into editing mode where it shows the
editing or reordering controls of each visible row, depending on the
editingStyle of each associated UITableViewCell. Clicking on the
insertion or deletion control causes the data source to receive a
tableView:commitEditingStyle:forRowAtIndexPath: message. You commit
a deletion or insertion by calling
deleteRowsAtIndexPaths:withRowAnimation: or
insertRowsAtIndexPaths:withRowAnimation:, as appropriate.
write this self.navigationItem.leftBarButtonItem = self.editButtonItem; in viewDidLoad method. After that implement this method - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[yourArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
}

Resources