05.编辑模式-删除

来源:互联网 发布:unity3d手游大全 编辑:程序博客网 时间:2024/06/05 18:55

-(void)loadView

{

   NSLog(@"开始了");

//    self.view.backgroundColor=[UIColor redColor];

    

    self.view=[[UIViewalloc] initWithFrame:[[UIScreenmainScreen] applicationFrame]];



   UIToolbar *toolbar=[[UIToolbaralloc] initWithFrame:CGRectMake(0,0, 320, 44)];

    [toolbar setTintColor:[UIColorredColor]];

    

    UIBarButtonItem *space=[[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpacetarget:nilaction:nil];



    //删除按钮

    UIBarButtonItem *item1=[[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:selfaction:@selector(remove)];

    

    [item1 setStyle:UIBarButtonItemStyleBordered];

    

    [toolbarsetItems:@[space,item1]];

    [self.viewaddSubview:toolbar];

    

    UITableView *tableView=[[UITableViewalloc] initWithFrame:CGRectMake(0,44, 320, 416)style:UITableViewStylePlain];

    [tableViewsetDataSource:self];

    [tableViewsetDelegate:self];

    [self.viewaddSubview:tableView];

   self.tableView=tableView;

}

-(void)remove{


   BOOL isEditing=[self.tableViewisEditing];

    

    [self.tableViewsetEditing:!isEditing animated:YES];

}

- (void)viewDidLoad {

    [superviewDidLoad];

    

   


    

    NSMutableArray *array = [NSMutableArrayarrayWithCapacity:50];

   for (NSInteger i =0; i < 50; i++) {

       NSString *str = [NSStringstringWithFormat:@"d", i];

        

        [arrayaddObject:str];

    }

   self.dataList = array;

     

}

#pragma mark - 数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return self.dataList.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell=[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:nil];

   NSString *str = self.dataList[indexPath.row];

    [cell.textLabelsetText:str];

    

   return cell;

}


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

{

    if(UITableViewCellEditingStyleDelete==editingStyle) {

        [self.dataListremoveObjectAtIndex:indexPath.row];

        [self.tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];

     

       NSLog(@"===%@==",self.dataList);

     }

}


- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

{

  return @"删除";

}

0 0