现在的位置: 首页 > 综合 > 正文

iPhone开发-ios7环境下Uitableview删除某一行后忽略点击事件

2018年02月14日 ⁄ 综合 ⁄ 共 1890字 ⁄ 字号 评论关闭

下面代码的作用是:当你点击选中tableview的某一行时,它将记录被选中的行。当你左扫并删除某一行时,它将删除该行数据并更新Uitableview中的数据。

@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]);
}

在ios6环境下,上段代码按照刚才描述的预期工作。但是在ios7环境下,我做了如下操作:当tableview中的一行被删除并更新tablview后,被删除行的下一行的点击事件将被忽略,导致点击该行无反应。很奇怪是不是,下面将解释原因。

当tableview中的某行被选中删除后,tableview将处于编辑(editing)状态,所以你需要将tableview中的状态更换成选择(selection)模式,更改代码如下:

- (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];
  }
}

注:本人水平有限,翻译错误在所难免,如有错误望指正,在下会尽快修改。

原文:http://stackoverflow.com/questions/19364409/uitableviewcontroller-ignores-tap-after-deleting-row-in-ios7

抱歉!评论已关闭.