Swipe to delete in UITableViewCell on a white background, need to clear

I am trying to change the background color of a view that appears when scrolling a UITableViewCell line, the background color behind the Delete button.

I tried changing cell.editingAccessoryView, but did nothing.

UIView* myBackgroundView = [[UIView alloc] initWithFrame:CGRectZero]; myBackgroundView.backgroundColor = [UIColor clearColor]; cell.editingAccessoryView = myBackgroundView; 

Any ideas?

+7
source share
6 answers

I answer this because it took me a while to find the answer, and this is the first entry that appears in the search. Using the willDisplayCell method, you can access the background color of the cells. Note that [UIColor clearColor]; will return white, so edit your code accordingly.

 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { cell.backgroundColor = [UIColor blackColor]; } 
+8
source

You are almost there. The UITableViewCell class has a backgroundView property, which is nil by default. Just create a new UIView , as you did in your question, then assign this to the backgroundView property of your cell.

 cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease]; cell.backgroundView.backgroundColor = [UIColor clearColor]; 
+7
source

I think it depends on how you add content to your cell.

When I added content to the cell directly using [cell addSubView] or [cell.contentView addSubView], I had the same problem.

My workaround:

 Create a view Add all your content(labels, images etc;) to this view Finally then add the view to your cell using [cell addSubView:tmpView] 

And you no longer need to interfere with the backgroundView property. I tried this and it works great!

+1
source

Although these answers are correct, I feel that in most cases it’s easier to just set the background color of the cell in the interface builder. By this, I mean the actual background color property of the cell, not the content view. There is no reason to do this dynamically if it is always the color that the presentation of the content has.

0
source

iOS8 adds a new method to UITableViewDelegate :

 optional func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? 

You can create a custom UITableViewRowAction that will use your row actions.

For instance:

 func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let deleteAction = UITableViewRowAction(style: .destructive, title: "Cancel", handler: { action, indexPath in // DELETE ROW HERE }) deleteAction.background = .green // you can change background color return [deleteAction] } 

For additional verification here and here

A good example of using the new API: here

0
source

right, just to let everyone know:

 func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) { for subview in tableView.subviews { if NSStringFromClass(type(of: subview)) == "UISwipeActionPullView", let button = subview.subviews.first, NSStringFromClass(type(of: button)) == "UISwipeActionStandardButton" { button.backgroundColor = .clear button.subviews.first?.backgroundColor = .clear } } } 
0
source

Source: https://habr.com/ru/post/904856/


All Articles