【问题标题】:UITableViewController ignores tap after deleting row in iOS7UITableViewController 在iOS7中删除行后忽略点击
【发布时间】:2013-10-14 16:04:19
【问题描述】:

考虑以下简单明了的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]);
}

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

【问题讨论】:

    标签: ios cocoa-touch uitableview ios7


    【解决方案1】:

    当您删除特定行时,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];
      }
    }
    

    【讨论】:

    • 谢谢你,如果我能给你 100 我的代表我会回答这个问题。
    猜你喜欢
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多