Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to present the delete button on right-to-left swipe in UITableView

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    if(editingStyle == UITableViewCellEditingStyleDelete){
        //[theSimpsons removeObjectAtIndex:indexPath.row];

        NSString *time = [[arrayList objectAtIndex:indexPath.row] objectForKey:@"TIME"];
        NSString *date= [[arrayList objectAtIndex:indexPath.row] objectForKey:@"DATE"];
        DBManager *dbm = [[DBManager alloc] init];
        [dbm initiallzeDatabase];
        [dbm deleteItemAtIndexPath:time :date];

        //arrayList = [dbm getParkResults];
        //[parkTableView reloadData];

        [arrayList removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

I have used the above code in my app. The editing part works fine, but the thing is, the delete button appears only when we swipe from left-to-right on a cell. Now i have a menu that opens on left-to-right swipe like Facebook. So how can i make sure that the delete button appears when the user swipes from right-to-left.

like image 805
ScarletWitch Avatar asked Mar 26 '13 07:03

ScarletWitch


People also ask

How do you customize swipe edit buttons in Uitableview?

As of iOS 8.0 there's an easy way to customize the list of buttons that appear when the user swipes from right to left: editActionsForRowAt . Return an array of UITableViewRowAction objects that have titles and styles (and also background colors if you want to customize their appearance), and iOS does the rest.

How do I delete a cell in Uitableview?

So, to remove a cell from a table view you first remove it from your data source, then you call deleteRows(at:) on your table view, providing it with an array of index paths that should be zapped. You can create index paths yourself, you just need a section and row number.

How do you delete a row in Tableview?

When a user slides horizontally across a row the editing style of the Tabel View Cell is set to delete. When the delete button is pressed, the item is deleted in the array and also the row is deleted in the Table View. Build and run the project and swipe-to-delete a row from the Table View.


1 Answers

Please dont set the delegate to UISwipeGestureRecognizer

Write this in viewDidLoad or where you have initiated your UITableView

 UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(callYourMethod:)];
 swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
 [self.tblView addGestureRecognizer:swipeRight];

No need to write anything in the below method.

- (void)callYourMethod:(UISwipeGestureRecognizer *)recognizer
{

}
like image 113
Manu Avatar answered Sep 22 '22 01:09

Manu