objective-c - 在 UITableViewCell 中的任意位置向左或向右滑动以删除没有删除按钮的单元格?

标签 objective-c ios uitableview

我希望能够在表格 View 单元格中的任意位置向左或向右滑动,以在不显示删除按钮的情况下删除带动画的单元格。我该怎么做?

最佳答案

我还没有尝试和实现这个,但我会试一试。首先,创建一个自定义的 UITableViewCell,并让它有 2 个你可以使用的属性

  1. 对正在使用它的 tableView 的引用。
  2. 用于在 tableView 中查找单元格的 indexPath。(请注意,每次您删除一个单元格时,都需要更新此单元格的所有单元格)

在您创建自定义单元格的 cellForRowAtIndexPath: 中,设置这些属性。同时向单元格添加一个 UISwipeGestureRecognizer

cell.tableView=tableView;
cell.indexPath=indexPath;
UISwipeGestureRecognizer *swipeGestureRecognizer=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(deleteCell:)];
[cell addGestureRecognizer:swipeGestureRecognizer];
[swipeGestureRecognizer release];

确保手势只接受水平滑动。

-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if([[gestureRecognizer view] isKindOfClass:[UITableViewCell class]]&&
       ((UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionLeft
        ||(UISwipeGestureRecognizer*)gestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight)) return YES;
}

在你的deleteCell:

-(void) deleteCell:(UIGestureRecognizer*)gestureRec
{
    UIGestureRecognizer *swipeGestureRecognizer=(UISwipeGestureRecognizer*)gestureRec;
    CustomCell *cell=[swipeGestureRecognizer view];
    UITableView *tableView=cell.tableView;
    NSIndexPath *indexPath=cell.indexPath;
    //you can now use these two to perform delete operation
}

关于objective-c - 在 UITableViewCell 中的任意位置向左或向右滑动以删除没有删除按钮的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9060032/

相关文章:

ios - 如何在 applicationWillTerminate 方法中调用 Web 服务并停止 VPN 服务器?

objective-c - Objective-C 中的 OpenSSL SHA256 等效项

ios - 解析 PFQueryTableViewController loadNextPage() 重新加载 tableView 问题

ios - tableview 图像上的视差,使用自动布局

iOS 7.1 beta5 tableviewcell 高度显示超出其范围的对象

objective-c - iOS Facebook SDK - 最佳实践?

iOS:处理 App Delegate 中的无效 session

ios - 更新 Xcode 中的框架和约束(Interface Builder)

ios - 有没有办法以通用方式替换关系的添加和删除方法的核心数据实现?

ios - 无法让 UILabel 显示超过 3 行