下面代码的作用是:当你点击选中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://blog.csdn.net/zcl369369/article/details/22323779
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!