【问题标题】:Want to present the delete button on right-to-left swipe in UITableView想要在 UITableView 中从右向左滑动显示删除按钮
【发布时间】:2013-03-15 22:15:16
【问题描述】:
- (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];
    }
}

我在我的应用程序中使用了上述代码。编辑部分工作正常,但问题是,只有当我们在单元格上从左向右滑动时才会出现删除按钮。现在我有一个菜单,可以像 Facebook 一样从左到右滑动打开。那么我如何确保当用户从右向左滑动时出现删除按钮。

【问题讨论】:

    标签: iphone objective-c uitableview


    【解决方案1】:

    请不要将代理设置为UISwipeGestureRecognizer

    将此写在viewDidLoad 或您发起UITableView 的地方

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

    下面的方法不需要写任何东西。

    - (void)callYourMethod:(UISwipeGestureRecognizer *)recognizer
    {
    
    }
    

    【讨论】:

    • 谢谢马诺哈尔。我通过结合你的想法和@Gossamer 的想法得到了答案。谢谢你们俩。你们为我节省了很多工作。 :)
    【解决方案2】:

    这可能有效。将此代码放入awakeFromNibinit

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

    然后在handleSwipeDelete 组织您的单元格并显示删除按钮。您可能需要处理单元格状态并在 swipeLeft/swipeRight 上更改它。

    【讨论】:

    • 我正在试一试。当您说显示删除按钮时,我们可以以编程方式显示默认删除按钮吗?如果是,那怎么办?谢谢:)
    • 您可以尝试将表格单元格类型设置为UITableViewCellEditingStyleDelete当向左滑动时。或者创建UITableViewCell 的子类并绘制该单元格而不是该行的默认单元格。
    【解决方案3】:

    看起来你需要自定义 UITableViewCell 的行为,我从来没有注意到苹果提供了从右向左滑动后的行为....

    【讨论】:

      【解决方案4】:

      你可以通过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
          }    
      }
      

      移除对象后。你必须重新加载UITableView

      [self.tableView reloadData];
      

      欲了解更多信息Read This OfficialDocumentaion.

      【讨论】:

      • 是的,谢谢@iPatel。但正如我所说。由于在同一事件中在菜单中滑动,因此无法读取该手势。所以这不起作用。但无论如何,谢谢。 :)
      猜你喜欢
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多