196,UITableView之cell增删移

来源:互联网 发布:java工程师面试技巧 编辑:程序博客网 时间:2024/06/02 04:32


#import "ViewController.h"


@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>


@property (nonatomic,strong)NSMutableArray *dataList;

@property (nonatomic,strong)UITableView *tableview;


@end


@implementation ViewController


-(NSMutableArray *)dataList{

    if (_dataList ==nil) {

        _dataList = [NSMutableArrayarrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",nil];

    }

    return_dataList;

}


-(UITableView *)tableview{

    if (_tableview ==nil) {

        _tableview = [[UITableViewalloc]initWithFrame:CGRectMake(0,20, self.view.bounds.size.width,self.view.bounds.size.height - 20) style:UITableViewStylePlain];

        _tableview.dataSource =self;

        _tableview.delegate =self;

        [self.viewaddSubview:_tableview];

    }

    return_tableview;

}



- (void)viewDidLoad {

    [superviewDidLoad];

    [selftableview];

    

    // 开始编辑,一旦editing == YES就默认开启删除模式

    self.tableview.editing =YES;

}


//设置每一个分组的行总数

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

    return self.dataList.count;

}


//设置单元格

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

    

    //设置重用ID

    static NSString *ID =@"cell";

    

    //从缓存中获取cell

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID];

    

    //如果缓存中没有cell,那么,就新实例化一个

    if (cell == nil) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ID];

    }

    

    //设置数据

    cell.textLabel.text =self.dataList[indexPath.row];

    

    return cell;

}


//只要实现了此方法,就能够支持手势拖拽删除了,删除需要自己干!

//UITableViewCellEditingStyleNone,

//UITableViewCellEditingStyleDelete, 删除

//UITableViewCellEditingStyleInsert  添加

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

    

    //其实,增加和删除,只要对模型中数据进行添加或删除,再刷新界面,就可以达到效果了

    if (editingStyle ==UITableViewCellEditingStyleDelete) {

        //1,从数据模型中移除数据

        [self.dataListremoveObjectAtIndex:indexPath.row];

        

        //2,刷新界面

        //[self.tableview reloadData];这种刷新方法,是界面全部数据刷新,很消耗性能

        //下面这种只是局部删掉数据行而已,不会刷新数据

        [self.tableviewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];

    }elseif(editingStyle ==UITableViewCellEditingStyleInsert){

        [self.dataListinsertObject:@"--"atIndex:indexPath.row +1];

         //[self.tableview reloadData];

        NSIndexPath *path = [NSIndexPathindexPathForRow:indexPath.row +1inSection:indexPath.section];

        [self.tableviewinsertRowsAtIndexPaths:@[path]withRowAnimation:UITableViewRowAnimationMiddle];

    }

    

}


//返回编辑样式,如果没有实现此方法,默认都是删除。返回UITableViewCellEditingStyleInsert,就是添加了

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

    if(indexPath.row <6){

        return UITableViewCellEditingStyleInsert;

    }else{

        returnUITableViewCellEditingStyleDelete;

    }

}


//拖动控件,只需要实现该方法

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

    //只要改变数据模型中的数据位置就可以了


    //1,从源从数组中取出

    id source = self.dataList[sourceIndexPath.row];

    

    //2,从源中删除数据

    [self.dataListremoveObjectAtIndex:sourceIndexPath.row];

    

    //3,将取出的数据插入数据模型中

    [self.dataListinsertObject:source atIndex:destinationIndexPath.row];

}

@end


0 0
原创粉丝点击