ios tableView那些事 (九) tableview的删除
2013-08-31 17:55
337 查看
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]];
}
第一种滑动方式,在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]];
}
相关文章推荐
- ios tableView那些事 (九) tableview的删除
- ios tableView那些事 (九) tableview的删除
- ios TableView那些事(三十一)UITableViewController ios8 删除时添加多个按钮
- ios tableView那些事 (九) tableview的删除
- ios TableView那些事3(Swift 三) TableView 的删除操作
- ios tableView那些事 (九) tableview的删除
- ios TableView那些事(三十一)UITableViewController ios8 删除时添加多个按钮
- IOS_修改TableView的删除按钮的文本
- iOS UITableView(十三) tableView经常用的代理总结和滑动删除
- ios tableView那些事 (十二) 给 tableview 加个长按快捷菜单
- ios tableview 那些事(二十七)tableview 下滑手势关闭键盘
- ios TableView那些事(四十)tableview 滚动到最底部
- iOS 使用cell的删除功能,实现tableview的代理
- IOS笔记(9)- tableView的左滑删除和多选删除
- ios TableView那些事(三十 四)TableView 单选操作
- ios tableView那些事(四)tableView添加标题,头尾和改变cell的宽度 分类: Ios tableview 2013-08-28 19:07 12096人阅读 评论(0) 收藏
- IOS tableView 滑动删除与排序功能
- ios tableView那些事 (十二) 给 tableview 加个长按快捷菜单
- ios tableView那些事(四)tableView添加标题,头尾和改变cell的宽度---tableview
- iOS开发系列 ---- UI (TableView的编辑和多项删除操作)