UITableViewEdit

来源:互联网 发布:txt阅读软件排行 编辑:程序博客网 时间:2024/05/03 20:18

#import "RootViewController.h"


@interface RootViewController ()<UITableViewDelegate,UITableViewDataSource>


@end


@implementation RootViewController

-(void)dealloc {

    [superdealloc];

}


- (void)viewDidLoad {

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorcyanColor];

    UITableView *tableView = [[UITableViewalloc] initWithFrame:self.view.boundsstyle:UITableViewStyleGrouped];

    tableView.delegate =self;

    tableView.dataSource =self;

    [tableView registerClass:[UITableViewCellclass] forCellReuseIdentifier:@"UITableViewCellIndentifer"];

    tableView.tag =1000;

    

//    系统默认自带一个编辑按钮 -可以直接赋值使用

    self.navigationItem.leftBarButtonItem =self.editButtonItem;

//    *****************************************************************



    [self.viewaddSubview:tableView];

    [tableViewrelease];

    

   _dataSource = [[NSMutableArrayalloc] initWithObjects:@"a",@"b", @"c",@"d", nil];

}

//重写 -- 系统自带编辑按钮的点击事件

-(void)setEditing:(BOOL)editing animated:(BOOL)animated {

    [supersetEditing:editing animated:animated];

//    通过tag获取tableView

   UITableView *tableView = (UITableView *)[self.viewviewWithTag:1000];

//    tableView处于可编辑状态

    [tableViewsetEditing:editing animated:animated];

}


//设置tableView的编辑样式

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

//    分为none/ 添加/ 删除

    

//    当为第一个cell的时候可编辑性为添加

   if (indexPath.row ==0) {

        returnUITableViewCellEditingStyleInsert;

    }

    returnUITableViewCellEditingStyleDelete;

}

//控制是否允许编辑的协议方法

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

//    YES可以编辑

//    NO不可以编辑

//    第二个cell不允许编辑

   if (indexPath.row ==1) {

       return NO;

    }

    return YES;

}


//需要tableView处于可编辑状态


//控制每一行是否可以移动

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

    return YES;

}


//移动完成执行的协议方法

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {

//    从数据源取值

   NSString *string = self.dataSource [sourceIndexPath.row];

//    从原来的位置删除

    [self.dataSourceremoveObjectAtIndex:sourceIndexPath.row];

//    插入到新的位置

    [self.dataSourceinsertObject:string atIndex:destinationIndexPath.row];

    [tableViewreloadData];

    

//    只刷新一个或者多个cell的方法

//    [tableView reloadRowsAtIndexPaths:@[sourceIndexPath] withRowAnimation:UITableViewRowAnimationRight];

    

}


//

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

//    当样式为添加的时候

    if (editingStyle ==UITableViewCellEditingStyleInsert) {

        [self.dataSourceaddObject:@"迪丽热巴"];

        [tableViewreloadData];

    }

    if (editingStyle ==UITableViewCellEditingStyleDelete) {

//       先处理数据

        [self.dataSourceremoveObjectAtIndex:indexPath.row];

//        [tableView reloadData];

//       再刷新tableView

        [tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];

    }

}



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

    return_dataSource.count;

}


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

    

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"UITableViewCellIndentifer"];

   if (_dataSource.count > indexPath.row) {

        cell.textLabel.text =_dataSource[indexPath.row];


    }

   return cell;

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end


1 0