下面代码的作用是:当你点击选中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]);
}当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];
}
}
注:本人水平有限,翻译错误在所难免,如有错误望指正,在下会尽快修改。
iPhone开发-ios7环境下Uitableview删除某一行后忽略点击事件,布布扣,bubuko.com
iPhone开发-ios7环境下Uitableview删除某一行后忽略点击事件
原文:http://blog.csdn.net/zcl369369/article/details/22323779