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).