First click on UITableView ignored after deleteRowsAtIndexPaths

My UITableView has a collection of cells. When scrolling through a cell, the "Delete" button appears. When you click this "Delete" button, the following code is executed:

NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; [self.itemList removeObjectAtIndex:indexPath.row]; 

This correctly causes the cell to be removed from the list. However, when I click on one of the remaining cells in UITableVIew to select it, this answer is ignored (i.e. TableView: didSelectRowAtIndexPath is not called). If I touch the second time, then it works correctly (for example, tableView: didSelectRowAtIndexPath is called).

It does not matter which cell I use after deletion, and it does not matter how long I wait after deletion, the first press after the deletion is always ignored, and the second press after deletion will be successful.

Based on various stackOverflow answers I tried:

  • self.tableView.editing = NO; after removal
  • call [self.tableView reloadData]; after removal
  • call [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade]; after removal
  • call [self.tableView beginUpdates]; before deleting and [self.tableView endUpdates]; after removal
  • execution of all of the above simultaneously

None of the above helped; the first press after deletion is always ignored.

UPDATE: I also added the code [self.itemList removeObjectAtIndex:indexPath.row]; into the code snippet above. My data source delegation methods are as follows:

 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.itemList.count; } 

The selected code fragment is called in the doDelete method in response to the button click ( [self.deleteButton addTarget:self action:@selector(doDelete) forControlEvents:UIControlEventTouchUpInside]; )

UPDATE # 2: based on Lyssa's comments, I tried the following: I deleted all the links to our custom Delete button and our swipe gesture, and then added this code to our UITableView delegate:

 -(BOOL) tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } -(void) tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [self.itemList removeObjectAtIndex:indexPath.row]; [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; } } 

This works - now I can delete the lines, and the next tap works correctly. However, this eliminates our custom look for our table cell, which was the goal of our custom code in the first place. Perhaps I should look at using the methods described above and setting the Delete button (I made some glances and have not yet found a good answer).

+7
ios objective-c uitableview
source share
2 answers

Thanks to everyone who provided answers or comments to my question. Your comments helped me identify the problem.

It turns out we implemented tableView:shouldHighlightRowAtIndexPath , and we returned NO for the first call after deletion, and then returned YES from now on. It is for this reason that the first answer was ignored. Our complete violation code was as follows:

 -(BOOL) tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath { if (self.currentPath) { [self resetSelectedCell]; self.currentPath = nil; return NO; } return YES; } 

This code was provided to display more than one delete button at a time, but we did not reset self.currentPath correctly after the removal. Now we set self.currentPath to zero after successful deletion, and the code works correctly.

0
source share

The problem is that when you use scroll to delete only the line you are scrolling enters edit mode.

And the first press finishes the editing mode for this line:

 - (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath { } 

you can solve this problem by setting edit mode

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [self.itemList removeObjectAtIndex:indexPath.row]; [self.tableView setEditing:NO animated:YES]; } [self.tableView reloadData]; } 
+4
source share

All Articles