gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 06:20:49 26 4
gpt4 key购买 nike

考虑以下简单明了的 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 中应该很容易重现问题。

最佳答案

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

关于ios - UITableViewController 在 iOS7 中删除行后忽略点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19364409/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com