Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewController ignores tap after deleting row in iOS7

Consider the following plain and simple UITableViewController: when you tap a row, it logs the selected row, and when you swipe and delete, it removes an item in the model and reloads the data.

@interface DummyTableViewController : UITableViewController

@property (nonatomic, strong) NSMutableArray *items;

@end

@implementation DummyTableViewController

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self)
    {
        _items = [ @[ @"A", @"B", @"C", @"D", @"E" ] mutableCopy];
    }
    return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.items count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    cell.textLabel.text = self.items[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self.items removeObjectAtIndex:indexPath.row];
        [tableView reloadData];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Row %@ tapped.", self.items[indexPath.row]);
}

In iOS6, this all works as expected, but in iOS7 I get the following behavior: after a row has been removed and the data has been reloaded, the first next tap on a table cell is ignored. It is only the second tap that triggers a table cell select again. Any idea what might be causing this or how to work around it? Issue should be easy to reproduce in iOS7 with the above code.

like image 382
hverlind Avatar asked Nov 18 '25 06:11

hverlind


1 Answers

The tableview is in editing state when you delete a particular row. SO you have to turn off the editing state to allow the tableView to go back to the selection mode. Change your code to this -

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (editingStyle == UITableViewCellEditingStyleDelete)
  {
    [self.items removeObjectAtIndex:indexPath.row];

    // Turn off editing state here
    tableView.editing = NO;


    [tableView reloadData];
  }
}
like image 161
Kedar Avatar answered Nov 20 '25 20:11

Kedar