Swipe to delete - iphone

I've looked and i can't seem to find any where on stack overflow some one that has had the same problem as me. So I use the following code:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete);
}
and when i swipe the delete button appears, but when pressed it doesn't do anything, what have i forgotten to do?

You need to actually delete your data after the if statement. Currently, your if statement does nothing at all, because it just has a semi-colon after it.
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
//Code to delete data goes here.
//This could include removing an object from an array, deleting it from core data,
//and removing the selected row.
}
}

Related

Left swipe for delete and edit,delete

I am not able to find a way around this. Is it possible to have delete option using left swipe and also using edit and delete.
I have edit button on the left of the navigation bar.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
and commitEditingStyle method as below
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//delete code here
}
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.
}
}
After using this, the left swipe is not being detected at all. the only way to delete seems to be by going to the edit mode and deleting from there.
help would be appreciated.
I am a newbie so please go easy with me :)
you mean swiping on cell you need delete option.
If so you can try this code:
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//perform delete operation
}
Make sure you have all of the delegates implemented below
tableView:commitEditingStyle:forRowAtIndexPath:
tableView:canEditRowAtIndexPath:
tableView:editingStyleForRowAtIndexPath
:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//delete code here
}
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.
}
once you write above method in your delegate you will be able to use swipe gestures.... and in this method you will handle the subsequent actions that you want to perform.
thanks.....

issue with swiping a UITableViewCell

I would like to do some stuff when a user swipes to the right of a UITableViewCell, so I use
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
However I tried to slide to the right and this didn't get invoked, why is this? All of my UITableView delegates are invoked.
I also have this in my code:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
All I want to do is to add a subview when a swipe happens
Do you have a tableView:commitEditingStyle:forRowAtIndexPath: method?
To quote Apple:
Note: A swipe motion across a cell does not cause the display of a Delete button
unless the table view's data source implements the
tableView:commitEditingStyle:forRowAtIndexPath: method.
And, deleting that method in my project also causes the willBeginEditing not to be called.
// You missed this?
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)theTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return #"Remove";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
}
}
Hope that helps.
Try adding this method to your tableView delegate methods:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
[self.listTableView beginUpdates];
...
I assume because your other delegate methods are being called that you included in your .h file #interface line? If using IB did you right click on the tableview and wire up delegate and dataSource?
Set the property of table view
tableView.editing = YES;

Swipe to Delete not working

The swipe to delete functionality is not working in my table view. I have implemented the commitEditingStyle delegate and the Edit button in the navigation bar. Hence when the user clicks the edit button, the delete and add buttons show up appropriately. However, on swiping, the delete button does not appear and it seems like it doesn't recognise the swipe as a call for the setEditing method.
I then implemented willBeginEditingRowAtIndexPath and didEndEditingRwoAtIndexPath delegates as follows:
-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"WILL BEGIN EDITING");
[self.tableView setEditing:YES animated:YES];
}
-(void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView setEditing:NO animated:YES];
}
However this does not have any effect either. What could be the possible issue? I have enabled multi-touch for the table view in the IB and each cell has an DetailDisclosureButton accessory.
For swipe-to-delete functionality, you have to implement
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
; // Delete.
}
EDIT: I didn't read the question well enough.
I have noticed that to do a swipe drag in the simulator I have to press and pause for a second while it selects the cell, and only then can I swipe right successfully.
You'll need to implement:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
With these three things you should be good to go. (I missed off the commitEditingStyle and it never worked.
If all other advices doesn't help try to check if you have pan gesture recognizer in your code. It will not work if you have things like this in some underlaying view.
I was doing the following in the tableView:editingStyleForRowAtIndexPath: delegate method :-
if (self.editing == NO || !indexPath)
{
return UITableViewCellEditingStyleNone;
}
This was supposed to return editing style none if no indexPath was selected. For some reason whenever a swipe action was performed, this particular if condition was getting executed. On commenting the above code snippet, the swipe to delete action worked fine.
Thanks all for your help.
The key to make it work is to implement:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
And remember to put it inside "UITableViewDataSource" instead of "UITableViewDelegate"
did you implement the tableView:editingStyleForRowAtIndexPath: method and have it return UITableViewCellEditingStyleDelete
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath delegate method also need to implement to work swipe functionality .

UITableView IndexPath.row out of scope

I am implementing the following tableview method:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
The problem is, when this gets hit, indexpath.row is showing up as out of scope. I am trying to handle the delete button for this row so I can delete the cell and delete the value out of the tableview's datasource.
What you are seeing is xCode's debugger being goofy. This happens a bunch. What I do is make a local variable like this and set my breakpoint there and the debugger will act better.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath temp = indexPath; //Set Debugger after this and you can inspect the variable.
}

How to disable the delete button for the particular row when we are using the table view delegate method commitEditingStyle in Iphone sdk?

Here I need a help from ur side that Im using the tableview delegate method commitEditing sytle to get the delete button for every cell when we swipe.But my problem here is I dont want the delete button for the first row in the tableview and swipe should not work and I dnt have any idea how to implemet this.
Anyone's help will be much appreciated.
Thank You,
Monish.
UITableViewCell has delete style by default. To change that you need to implement tableView:editingStyleForRowAtIndexPath: method in your table view delegate. Return UITableViewCellEditingStyleNone for the 1st row and UITableViewCellEditingStyleDelete for other rows.
Edit: Sorry for incomplete answer. You also need to implement tableView:canEditRowAtIndexPath: method (something like that):
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return indexPath.row > 0;
}
Hope that'll work
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(condition check)
return UITableViewCellEditingStyleDelete;
return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//here your code
}
}