Redrawing UITableViewCell when entering / exiting edit mode

I have a table view in which cells are built differently depending on whether the table is being edited or not. In particular, the selection style is missing in edit mode and blue, if not in edit mode.

When I move from one to another, I noticed that some of the cells are not updated. A quick bit of logging tells me that even if the appearance of the cells changes quite dramatically (accessory views are added / deleted correctly, for example), the table view does not update the selection style (and, not important, the text).

What's going on here? Are only some cell attributes updated when setEditing is called? Presumably, only those who have a specific method to highlight a separate presentation style (for example, EditingAccessoryType)? I guess I will like EditingSelectionStyle.

How to resolve it? By setting setEditing to change the Style selection for each cell? I'm not even sure how I will go through the table view to do this. reloadData is not an option due to some animation I use.

+6
source share
3 answers

I found that setting setEditing: to iterate the visible cells and set the selectStyle for each element to work fine.

- (void)setEditing:(BOOL)editing animated:(BOOL)animated{ [super setEditing:editing animated:animated]; for (UITableViewCell *cell in [self.tableView visibleCells]) { NSIndexPath *path = [self.tableView indexPathForCell:cell]; cell.selectionStyle = (self.editing && (path.row > 1 || path.section == 0)) ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleBlue; } } 
+6
source

If you look at the UITableViewDelegate documentation , you will see that there are five ways to customize the editing behavior. There is also a method

  - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 

in the UITableViewDataSource documentation , which will be called in each cell before you go into edit mode. The same is true for

 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 

which will be called for all editable cells. If you want to change the way you view cells, you can do this in any of them. (Without implementing canEditRow.. , it is assumed that all lines are editable.)


Also note that there may be other ways to enter edit mode, such as scrolling through a cell, in which case

  - (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath 

will be called for the cell you clicked on:

When you enter this edit mode, "swipe the screen" in the table view, the message tableView: willBeginEditingRowAtIndexPath: is sent to the delegate so that he can customize his user interface.

+2
source

This works on Swift 2.3 by simply rewriting the setEditing method in your custom cell subclass:

 class MyCell: UITableViewCell { override func setEditing(editing: Bool, animated: Bool) { super.setEditing(editing, animated: animated) //Place your code here... } } 
0
source

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


All Articles