How to enable scroll to delete a cell in TableView?

I have a UIViewController that implements the delegate protocols TableViews and datasource . Now I want to add “swipe to remove” gestures to the cells.

How should I do it.

I gave an empty implementation of the commitEditingStyle method, and set the Edit property to YES.

However, the swipe function is not suitable.

Now I need to separately add a UISwipeGesture to each cell?

Or am I missing something?

+72
ios uitableview uiswipegesturerecognizer
Jan 24 '12 at 6:59
source share
13 answers

You do not need to set editing:YES if you need to display the "Delete" button when scrolling through a cell. You must implement tableView:canEditRowAtIndexPath: and return the rows you need to edit / delete from there. This is not necessary if your tableView dataSource is a subclass of UITableViewContoller - this method, if not overridden, returns YES by default. In all other cases, you must implement it.

EDIT:. Together we found a problem - tableView:editingStyleForRowAtIndexPath: returned UITableViewCellEditingStyleNone if the table was not in edit mode.

+51
Jan 24 2018-12-12T00:
source share

As Dan already noted, you need to implement the following delegation methods for table views:

  • tableView:canEditRowAtIndexPath:
  • tableView:commitEditingStyle:forRowAtIndexPath:

Note. I tried this on iOS 6 and iOS 7.

 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return YES - we will be able to delete all rows return YES; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // Perform the real delete action here. Note: you may need to check editing style // if you do not perform delete only. NSLog(@"Deleted row."); } 
+59
07 Oct '13 at 9:20
source share
 // Override to support conditional editing of the table view. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not 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) { // Delete the row from the data source [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } 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 } } 
+25
Dec 15 '13 at 0:01
source share

Please try this code quickly,

 override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { // let the controller to know that able to edit tableView row return true } override func tableView(tableView: UITableView, commitEditingStyle editingStyle UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { // if you want to apply with iOS 8 or earlier version you must add this function too. (just left in blank code) } override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { // add the action button you want to show when swiping on tableView cell , in this case add the delete button. let deleteAction = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action , indexPath) -> Void in // Your delete code here..... ......... ......... }) // You can set its properties like normal button deleteAction.backgroundColor = UIColor.redColor() return [deleteAction] } 
+12
Jul 08 '15 at 16:16
source share

Try adding the following to your class:

 // Override to support conditional editing of the table view. - (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return(YES); } 
+5
Jan 24 '12 at 7:08
source share

Conclusion Kir Dunenkoff Chat

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

should not be determined if you need the delete button to appear when scrolling.

+3
Jun 14 '13 at 18:10
source share

It was a problem for me too ... I could only scroll to delete, to work every 10 or so attempts. It turns out that the gesture on the TV is blocked by another gesture in the parent view controller. The TV was embedded in the MMDrawerController (make-up drawer layout).

Just set up the gesture recognizer in the drawer controller so as not to respond to cramped gestures in the flanks allowed for deletion to work on my TV.

You can also try to do something like this using gesture delegate :

 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } 
0
May 6 '14 at 22:13
source share

This is a quick version.

 // Override to support conditional editing of the table view. override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { // Return NO if you do not want the specified item to be editable. return true } // Override to support editing the table view. override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { // Delete the row from the data source tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } else if editingStyle == .Insert { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view } } 
0
May 6 '15 at 9:22
source share

If you use NSFetchedResultsControllerDelegate to populate the table view, this worked for me:

  • Make sure tableView:canEditRowAtIndexPath always returns true
  • In your implementation of tableView:commitEditingStyle:forRowAtIndexPath do not remove the row directly from the table view. Instead, delete it using the context of the managed entity, for example:

     if editingStyle == UITableViewCellEditingStyle.Delete { let word = self.fetchedResultsController.objectAtIndexPath(indexPath) as! Word self.managedObjectContext.deleteObject(word) self.saveManagedObjectContext() } func saveManagedObjectContext() { do { try self.managedObjectContext.save() } catch { let saveError = error as NSError print("\(saveError), \(saveError.userInfo)") } } 
0
Feb 17 '16 at 22:05
source share

In my experience, it looks like you should have editing on a UITableView set to NO to scroll to work.

self.tableView.editing = NO;

0
Jun 09 '16 at 16:17
source share

After iOS 8.0, you can configure your action in

 - (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath 
0
Jan 04 '17 at 7:03 on
source share
 NSMutableArray *post= [NSMutableArray alloc]initWithObject:@"1",@"2",@"3",nil]; - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger row = [indexPath row]; NSUInteger count = [posts count]; if (row < count) { return UITableViewCellEditingStyleDelete; } else { return UITableViewCellEditingStyleNone; } } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger row = [indexPath row]; NSUInteger count = [posts count]; if (row < count) { [posts removeObjectAtIndex:row]; } } 
-one
Oct. 20 '14 at 4:59
source share

You can see all the necessary methods by creating the UITableViewController (temporary) class in Xcode 5, and then copy the method you would like to use. The methods you need will be commented out with the pre-populated lines you want.

-2
Apr 23 '14 at
source share



All Articles