UITableViewController在iOS7中删除行后忽略点击

hve*_*ind 3 cocoa-touch uitableview ios ios7

请考虑以下简单明了UITableViewController:当您点击一行时,它会记录所选行,当您滑动和删除时,它会删除模型中的项目并重新加载数据.

@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]);
}
Run Code Online (Sandbox Code Playgroud)

在iOS6中,这一切都按预期工作,但在iOS7中我得到以下行为:删除一行并重新加载数据后,将忽略表格单元格的第一次下一次点击.只有第二次敲击再次触发表格单元格选择.知道可能导致这个或如何解决它的原因是什么?使用上面的代码,问题应该很容易在iOS7中重现.

Ked*_*dar 11

删除特定行时,tableview处于编辑状态.因此,您必须关闭编辑状态以允许tableView返回选择模式.将您的代码更改为此 -

- (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];
  }
}
Run Code Online (Sandbox Code Playgroud)