您的位置:首页 > 其它

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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: