tableview 的删除一定是很常用的在应用里! 在应用里大多会用到3中删除方式!
第一种滑动方式,在cell 的最右边向右滑动cell ,默认的删除是汉字,我们先改下语言吧!怎么也的改成国语!
这是常用的方式!也是必须用到的方式
上一章我们设置了不出现红色按钮
下面设置可以出现删除按钮 或者直接不写这个方法
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
/*改变删除按钮的title*/
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"删除";
}
/*删除用到的函数*/
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle ==UITableViewCellEditingStyleDelete)
{
[self.arrayValue removeObjectAtIndex:[indexPathrow]]; //删除数组里的数据
[tableview deleteRowsAtIndexPaths:[NSMutableArrayarrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationAutomatic]; //删除对应数据的cell
}
}
第二中是在导航栏上加个删除按键!为了有的用户不知道删除手势!这样也更加明了
就像上一章插入的方式一样! 只要让tableview可以编辑就会出现删除按钮
UIBarButtonItem *deleteButton = [[UIBarButtonItemalloc]initWithTitle:@"管理"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(deleteAction)];
[self.navigationItem.rightBarButtonItemsetTitleTextAttributes:[NSDictionarydictionaryWithObjectsAndKeys:[UIFontboldSystemFontOfSize:14],NSFontAttributeName, [UIColorredColor],NSForegroundColorAttributeName,nil]forState:UIControlStateNormal];
-(void)deleteAction
{
[self.tableviewsetEditing:!self.tableview.editinganimated:YES];
if (self.tableview.editing)
{
[self.navigationItem.leftBarButtonItemsetTitle:@"删除"];
}
else
{
[self.navigationItem.leftBarButtonItemsetTitle:@"管理"];
}
}
效果如下
第三种是在一个前辈博客rainbird.blog 中学到的一种批量删除方法!
deleteDic = [[NSMutableDictionaryalloc]init];
UIBarButtonItem *delBtn = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrashtarget:selfaction:@selector(delbtnClick)];
UIBarButtonItem *editBtn = [[UIBarButtonItemalloc]initWithTitle:@"编辑"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(editbtnClick)];
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
returnUITableViewCellEditingStyleDelete |UITableViewCellEditingStyleInsert;
}
//删除按钮
-(void)delbtnClick
{
[arrayremoveObjectsInArray:[deleteDicallKeys]];
[arraywriteToFile:Pathatomically:YES];
[self.mytableViewdeleteRowsAtIndexPaths:[NSArrayarrayWithArray:[deleteDicallValues]]withRowAnimation:UITableViewRowAnimationFade];
[deleteDicremoveAllObjects];
[array release];
}
//编辑按钮
-(void)editbtnClick
{
if ([self.navigationItem.rightBarButtonItem.titleisEqual:@"编辑"])
{
self.navigationItem.rightBarButtonItem.title =@"确定";
[self.mytableViewsetEditing:YESanimated:YES];
}
else
{
self.navigationItem.rightBarButtonItem.title =@"编辑";
[deleteDicremoveAllObjects];
[self.mytableViewsetEditing:NOanimated:YES];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.navigationItem.rightBarButtonItem.titleisEqual:@"确定"])
{
[deleteDicsetObject:indexPathforKey:[arrayobjectAtIndex:indexPath.row]];
}