Muhammad Umar
Muhammad Umar

Reputation: 11782

Showing delete button on swipe uitableviewcell like favourites

I am using following code

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

-(void)swipePressed:(UISwipeGestureRecognizer *)gestureRecognizer
{
    CGPoint p = [gestureRecognizer locationInView:self.myTable];
    NSIndexPath *indexPath = [self.myTable indexPathForRowAtPoint:p];
    if (indexPath == nil)
        NSLog(@"long press on table view but not on a row");
    else
    {
        [[self.myTable cellForRowAtIndexPath:indexPath] setEditingAccessoryType: UITableViewCellEditingStyleDelete];
    }
}

the swipePressed is run but no Delete button shows up...

Upvotes: 0

Views: 387

Answers (1)

dalton_c
dalton_c

Reputation: 7171

You do not need to install a swipe gesture recognizer in order to implement the swipe-to-delete functionality of a UITableView. This functionality is provided to you for free by the UITableView delegate. That is also not the correct use of setEditingAccessoryType. Remove the swipe gesture recognizer and method completely, then implement the method:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

Upvotes: 2

Related Questions