滑动以仅通过一个手势删除单元格

时间:2016-02-15 12:38:20

标签: ios uitableview

我想通过一个滑动手势删除单元格。问题是当我向左滑动时会出现删除图标。当然,当我点击删除图标时,单元格将被删除。我想在滑动手势后立即删除单元格。它由ios 9支持?

更多详情 当用户向左滑动到单元格的中间时,将出现删除按钮。当他继续滑动到屏幕边缘时,将删除单元格。

3 个答案:

答案 0 :(得分:2)

为什么没有自定义表格单元格

在单元格上添加内容视图或任何部分的滑动手势,

使用indexpath

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

答案 1 :(得分:2)

在UITableView上使用UISwipeGesture:

   - (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];
        }

答案 2 :(得分:2)

我为你的问题尝试了解决方案。很容易就得到了解决方案。

的.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

以上我的回答非常有效。