TableView中的编辑删除功能

来源:互联网 发布:网络安全法培训课件 编辑:程序博客网 时间:2024/05/16 13:02

简单的滑动删除比较简单,只要打开下面这个方法就行

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath


对于编辑删除,也很简单,右上角添加一个“编辑”即可。

例子代码如下:

- (void)viewDidLoad

{

    [superviewDidLoad];

        

   UIBarButtonItem *editButton = [[UIBarButtonItemalloc]

 initWithTitle:@"编辑"

 style:UIBarButtonItemStyleDone

 target:self

 action:@selector(edit)];

self.navigationItem.rightBarButtonItem = editButton;

    _tag = YES;

    

}

-(void) edit{

if (_tag ==YES) {

[self.tableViewsetEditing:YESanimated:YES];

//设置导航栏上右边的编辑按钮

UIBarButtonItem *editButton = [[UIBarButtonItemalloc]

 initWithTitle:@"完成"

 style:UIBarButtonItemStyleBordered

 target:self

 action:@selector(edit)];

self.navigationItem.rightBarButtonItem = editButton;

_tag =NO;

}else {

[self.tableViewsetEditing:NOanimated:YES];

//设置导航栏上右边的编辑按钮

UIBarButtonItem *editButton = [[UIBarButtonItemalloc]

 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

        [tableViewdeleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        DragBasicData *drugData = [[DragBasicDataalloc] init];

        drugData = [self.dataControllerobjectInListAtIndex:indexPath.row];         

        [self.dataControllerdeleteBookMarkById:drugData.strDragId]; 

        [selfrefreshDataList];

    }   

   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 = [[DragBasicDataListalloc] initBookMarks];     

    [[selftableView] reloadData];

}



原创粉丝点击