change table view cell background color while swipe to delete

Change background color of Cell on swipe to delete

Is it possible to change background color of cell when user swipes it to get the delete button. I have searched a lot for it but did not get any solution.

 

This will help you:

-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath*)indexPath
{UITableViewCell*cell =[tableView cellForRowAtIndexPath:indexPath];
  cell.backgroundColor =[UIColor redColor];}

According to official documentation here, this UITableViewDelegate method is tailor made for such kind of work.

In this “swipe to delete” mode the table view does not display any insertion, deletion, and reordering controls. This method gives the delegate an opportunity to adjust the application’s user interface to editing mode.

EDIT:: According to the comment you want to capture the event when user does not delete the cell but swipe to end the editing mode. For this, we have the following delegate:

-(void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath*)indexPath
{UITableViewCell*cell =[tableView cellForRowAtIndexPath:indexPath];
      cell.backgroundColor = originalColor;}

And If you would like to change delete button background color, there are two ways:

1.  use third party called  “MSCMoreOptionTableViewCell”

2.  use following method:

/*
 * Looks for a UIDeleteConfirmationButton in a given UIDeleteConfirmationView.
 * Returns nil if the button could not be found.
 */
– (UIButton *)deleteButtonFromDeleteConfirmationView:(UIView *)deleteConfirmationView {
    for (UIButton *deleteConfirmationButton in deleteConfirmationView.subviews) {
        NSString *name = NSStringFromClass([deleteConfirmationButton class]);
        if ([name hasPrefix:@”UI”] && [name rangeOfString:@”Delete”].length > 0 && [name hasSuffix:@”Button”]) {
            return deleteConfirmationButton;
        }
    }
    returnnil;
}

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s