UITableView loses choice

I have a problem with choosing a UITableView. If I click the edit button, TableView goes into edit mode and loses the current selection.

How can I go into edit mode without losing selection?

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the specified item to be editable. return YES; } 
+4
source share
2 answers

I was able to reproduce it: if you select a row like this [self.tableView selectRowAtIndexPath:] , the cell loses its selection. But if you select the cell directly, it will not lose the selection when editing, something like this:

  UITableViewCell* cell = [self.tableView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:1 inSection:0]]; [cell setSelected:YES animated:NO]; 
+2
source

Override setEditing:animated: in the table view controller. If you go into edit mode, save the current selection as an instance variable of NSIndexPath:

 selectedRow = [self.tableView indexPathForSelectedRow]; 

If you exit edit mode, set the table selection to the saved index path:

 [self.tableView selectRowAtIndexPath:selectedRow animated:NO scrollPosition:UITableViewScrollPositionNone]; 

Be careful not to try and select the line that was deleted during editing. And don't forget to name the implementation of super !

+1
source

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


All Articles