How to enable swipe to delete cell in a TableView?

后端 未结 13 2615
悲哀的现实
悲哀的现实 2020-12-07 21:40

I have a UIViewController that implements TableViews delegate and datasource protocols. Now I want to add \"swipe to delete\" gesture to cells.

相关标签:
13条回答
  • 2020-12-07 22:15

    This is the swift version

    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the specified item to be editable.
        return true
    }
    
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    
    0 讨论(0)
  • 2020-12-07 22:15

    This was a problem for me too...I could only get swiping to delete to work once every 10 or so attempts. It turns out the gesture on the TV was being blocked by another gesture in the parent view controller. The TV was nested in a MMDrawerController (swipe able drawer layout).

    Just configuring the gesture recognizer in the drawer controller to not respond to close gestures in the flanking drawers allowed swipe to delete to work in my TV.

    You could also try doing something like this with the gesture delegate:

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
        return YES;
    }
    
    0 讨论(0)
  • 2020-12-07 22:18

    You can see all necessary methods by creating a UITableViewController class (temporary) in XCode 5 and then copy which method you would like to use. Those methods you need will be commented out with pre-filled in lines you desire.

    0 讨论(0)
  • 2020-12-07 22:23
    NSMutableArray *post= [NSMutableArray alloc]initWithObject:@"1",@"2",@"3",nil]; 
    
    
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
               editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        NSUInteger row = [indexPath row];
        NSUInteger count = [posts count];
    
        if (row < count) {
            return UITableViewCellEditingStyleDelete;
        } else {
            return UITableViewCellEditingStyleNone;
        }
    }
    
    - (void)tableView:(UITableView *)tableView 
                        commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
                        forRowAtIndexPath:(NSIndexPath *)indexPath {
    
        NSUInteger row = [indexPath row];
        NSUInteger count = [posts count];
    
        if (row < count) {
    
            [posts removeObjectAtIndex:row];
        }
    }
    
    0 讨论(0)
  • 2020-12-07 22:27
    // Override to support conditional editing of the table view.
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Return NO if you do not 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) {
            // Delete the row from the data source
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }   
        else if (editingStyle == UITableViewCellEditingStyleInsert) {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }   
    }
    
    0 讨论(0)
  • 2020-12-07 22:27

    In my experience, seems like you must have editing on UITableView set to NO for swiping to work.

    self.tableView.editing = NO;

    0 讨论(0)
提交回复
热议问题