Bhumit Mehta
Bhumit Mehta

Reputation: 16318

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.

Thanx in advance.

Upvotes: 2

Views: 2895

Answers (1)

Puneet Sharma
Puneet Sharma

Reputation: 9484

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

Upvotes: 4

Related Questions