TableView中的编辑删除功能

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

- (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)