Want to submit a delete button from right to left in a 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 used the above code in my application. Part of the editing works fine, but the fact is that the delete button appears only when scrolling from left to right in a cell. Now I have a menu that opens from left to right, like Facebook. So, how can I make sure that the delete button appears when the user clicks from right to left.

+6
source share
4 answers

It might work. Put this code in awakeFromNib or init :

 UISwipeGestureRecognizer *swipeDelete = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeDelete:)]; [swipeDelete setDirection: UISwipeGestureRecognizerDirectionLeft]; [yourTableView addGestureRecognizer:swipeDelete]; 

then in handleSwipeDelete organize your cell and click the delete button. You will probably need to process the state of the cell and change it to swipeLeft / swipeRight.

+2
source

Please do not set the delegate to UISwipeGestureRecognizer

Write this in viewDidLoad or where you started your UITableView

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

There is no need to write anything in the method below.

 - (void)callYourMethod:(UISwipeGestureRecognizer *)recognizer { } 
+6
source

You can get it using two Methods of UITableVie.

 // This only needs to be implemented if you are going to be returning NO // for some items. By default, all items are editable. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return YES if you want the specified item to be editable. return YES; } // Override to support editing the table view. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { //add code here for when you hit delete //When you remove a cell of your tableview, you also has to remove your array object at index x } } 

After deleting the object. You must reload the UITableView .

 [self.tableView reloadData]; 

For more information Read this white paper .

+1
source

it looks like you need to customize the behavior of the UITableViewCell, I never noticed that Apple provided the behavior after scrolling from right to left ....

0
source

All Articles