TableView中的编辑删除功能
2012-04-01 16:42
405 查看
简单的滑动删除比较简单,只要打开下面这个方法就行
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
对于编辑删除,也很简单,右上角添加一个“编辑”即可。
例子代码如下:
- (void)viewDidLoad
{
[super
viewDidLoad];
UIBarButtonItem *editButton = [[UIBarButtonItem
alloc]
initWithTitle:@"编辑"
style:UIBarButtonItemStyleDone
target:self
action:@selector(edit)];
self.navigationItem.rightBarButtonItem
= editButton;
_tag = YES;
}
-(void) edit{
if (_tag ==
YES) {
[self.tableView
setEditing:YES
animated:YES];
//设置导航栏上右边的编辑按钮
UIBarButtonItem *editButton = [[UIBarButtonItem
alloc]
initWithTitle:@"完成"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(edit)];
self.navigationItem.rightBarButtonItem
= editButton;
_tag =
NO;
}else {
[self.tableView
setEditing:NO
animated:YES];
//设置导航栏上右边的编辑按钮
UIBarButtonItem *editButton = [[UIBarButtonItem
alloc]
initWithTitle:@"编辑"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(edit)];
self.navigationItem.rightBarButtonItem
= editButton;
_tag =
YES;
}
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle ==
UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView
deleteRowsAtIndexPaths:[NSArray
arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
DragBasicData *drugData = [[DragBasicData
alloc] init];
drugData = [self.dataController
objectInListAtIndex:indexPath.row];
[self.dataController
deleteBookMarkById:drugData.strDragId];
[self
refreshDataList];
}
else
if (editingStyle ==
UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath
*)indexPath __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0){
return @"删除";
}
- (void)refreshDataList{
self.dataController = [[DragBasicDataList
alloc]
initBookMarks];
[[self
tableView] reloadData];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
对于编辑删除,也很简单,右上角添加一个“编辑”即可。
例子代码如下:
- (void)viewDidLoad
{
[super
viewDidLoad];
UIBarButtonItem *editButton = [[UIBarButtonItem
alloc]
initWithTitle:@"编辑"
style:UIBarButtonItemStyleDone
target:self
action:@selector(edit)];
self.navigationItem.rightBarButtonItem
= editButton;
_tag = YES;
}
-(void) edit{
if (_tag ==
YES) {
[self.tableView
setEditing:YES
animated:YES];
//设置导航栏上右边的编辑按钮
UIBarButtonItem *editButton = [[UIBarButtonItem
alloc]
initWithTitle:@"完成"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(edit)];
self.navigationItem.rightBarButtonItem
= editButton;
_tag =
NO;
}else {
[self.tableView
setEditing:NO
animated:YES];
//设置导航栏上右边的编辑按钮
UIBarButtonItem *editButton = [[UIBarButtonItem
alloc]
initWithTitle:@"编辑"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(edit)];
self.navigationItem.rightBarButtonItem
= editButton;
_tag =
YES;
}
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle ==
UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView
deleteRowsAtIndexPaths:[NSArray
arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
DragBasicData *drugData = [[DragBasicData
alloc] init];
drugData = [self.dataController
objectInListAtIndex:indexPath.row];
[self.dataController
deleteBookMarkById:drugData.strDragId];
[self
refreshDataList];
}
else
if (editingStyle ==
UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath
*)indexPath __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0){
return @"删除";
}
- (void)refreshDataList{
self.dataController = [[DragBasicDataList
alloc]
initBookMarks];
[[self
tableView] reloadData];
}
相关文章推荐
- iOS 开发-实现TableView的编辑功能以删除数据
- iOS中tableview实现编辑、全选及删除等功能的方法示例
- ios tableView编辑模式下cell中创建多个功能
- ios 如何让TableView加入横向滑动出现删除按钮的功能
- IOS 表视图(UITableVIew)的使用方法(5)表视图的编辑功能(删除)
- editActionsForRowAtIndexPath(iOS8) tableview编辑(删除、插入、移动)
- iOS tableview上textView在编辑状态时,tableview自动上移的功能
- [11]UITableView编辑 删除 添加 移动 单例 及其去掉TableView没内容的Cell
- Swift - tableView的单元格多选功能的实现(获取多选值、多选删除)
- IOS UITableViewUITableView小技巧--实现cell向左滑动删除,编辑等功能
- iOS 嵌套在scrollView里面的tableView实现左滑删除功能
- iOS tableView 如何禁止滑动手势删除,只允许在编辑模式下进行删除Cell
- 关于tableView多选删除的编辑设置
- CYC- tableView的删除编辑移动
- plist文件的读取和tableView的编辑(插入和删除)
- iOS tableView编辑删除(单选 多选)
- iOS开发之tableView实现左滑删除功能
- 编辑表格@tableView实现增加、删除、移动的操作
- iOS中tableview 中编辑,删除与多选中的问题(一)
- iOS之tableView(五)的编辑删除插入操作和UIAlertController的使用