Tomasz
Tomasz

Reputation: 1416

Swipe to delete cell by only one gesture

I would like to delete cell by one swipe gesture. The problem is that when I slide left delete icon will appear. Of course when I click on delete icon cell will be deleted. I would like to delete cell immediately after swipe gesture. It is supported by ios 9?

More details When the user slide left to the middle of the cell delete button will appear. When he will continue swiping to the edge of the screen cell will be deleted.

Upvotes: 1

Views: 143

Answers (3)

user3182143
user3182143

Reputation: 9589

I tried the solution for your question.Very easily I got the solution.

.m

 #import "ViewController.h"

@interface ViewController ()
{
   NSMutableArray *arrayData;
}

@end

@implementation ViewController

@synthesize tableViewSwipeDelete;

- (void)viewDidLoad 
{
    [super viewDidLoad];
   // Do any additional setup after loading the view, typically from a nib.
    arrayData = [[NSMutableArray alloc]initWithObjects:@"iOS",@"Android",@"Windows",@"Tablet",@"iPAD", nil];
    UISwipeGestureRecognizer *gestureDeleteRow = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellSwipe:)];
    gestureDeleteRow.direction = UISwipeGestureRecognizerDirectionLeft;
   [tableViewSwipeDelete addGestureRecognizer:gestureDeleteRow];
}

-(void)cellSwipe:(UISwipeGestureRecognizer *)gesture
{
   CGPoint location = [gesture locationInView:tableViewSwipeDelete];
   NSIndexPath *swipedIndexPath = [tableViewSwipeDelete indexPathForRowAtPoint:location];
  //Delete Row…
  [arrayData removeObjectAtIndex:swipedIndexPath.row];
  [tableViewSwipeDelete deleteRowsAtIndexPaths:[NSArray arrayWithObjects:swipedIndexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
}

- (void)didReceiveMemoryWarning 
{
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return arrayData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *strCell = @"cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
  if(cell==nil)
  {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
  }
  cell.textLabel.text = arrayData[indexPath.row];
  return cell;
}
@end

Above my answer works perfectly.

Upvotes: 2

iAnurag
iAnurag

Reputation: 9356

Use UISwipeGesture on UITableView:

   - (void)viewDidLoad
   {
    [super viewDidLoad];
    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:sel
                                                                               action:@selector(leftSwipe:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [self.tableView addGestureRecognizer:recognizer];
    }


 - (void)leftSwipe:(UISwipeGestureRecognizer *)gestureRecognizer
        {
             CGPoint location = [gestureRecognizer locationInView:self.tableView];
            NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location]; 
            [dataSourceArray removeObjectAtIndex:indexPath.row];
            [tableView reloadData];
        }

Upvotes: 2

Devanshu Saini
Devanshu Saini

Reputation: 775

Why don't you have a custom table cell

On the cell add swipe gesture on the content view or any portion,

Delegate its call to the tableviewcontroller with the indexpath

- (void)someDelegateFunctionToDeleteCellAtIndexPath:(NSIndexpath *)indexPath{
[dataSourceArray removeObjectAtIndex:indexPath.row];
NSArray *deleteIndexPaths = @[indexPath];
  [tableView beginUpdates];
    [tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];  
  [tableView endUpdates];
}

Upvotes: 2

Related Questions