【问题标题】:Swipe to delete cell by only one gesture只需一个手势滑动即可删除单元格
【发布时间】:2016-02-15 12:38:20
【问题描述】:

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

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

【问题讨论】:

  • 你能把你的问题解释清楚吗?
  • @user3182143 我更新了我的问题。

标签: ios uitableview


【解决方案1】:

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

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

使用索引路径将其调用委托给 tableviewcontroller

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

【讨论】:

    【解决方案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];
            }
    

    【讨论】:

      【解决方案3】:

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

      .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
      

      以上我的回答完美。

      【讨论】:

        猜你喜欢
        • 2021-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多