iOS-UITableView的增加、移除

 


#import "ViewController.h"


@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

{

    UITableView *_tableView;

    

//    tableView 的数据源

    NSMutableArray *dataArray;

    

//    存放从plist文件中读取的内容

    NSMutableDictionary *dict;

    

//    用来给tableView的编辑器设置不同的状态

    BOOL isInsert;

}

@end


@implementation ViewController

/*

 关于UITableView的调错方法

 1、通过打断点检查代理方法是否执行,如果代理方法执行,认真检查代理方法里的代码

 2、如果代理方法不执行,检查是否遵守协议和挂代理

 3、检查数据源是否有数据以及数据格式是否正确

 */

- (void)viewDidLoad {

    [super viewDidLoad];

    

    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 414, 480) style:UITableViewStylePlain];

    

    _tableView.backgroundColor = [UIColor colorWithWhite:0.800 alpha:1.000];

    

//    找到plist文件的路径

    NSString *path = [[NSBundle mainBundle]pathForResource:@"Property List" ofType:@"plist"];

    

//    通过路径找到字典

    dict = [NSMutableDictionary dictionaryWithContentsOfFile:path];

    NSLog(@"%@=",dict);

    

//    取出dict中的dataArray数组,并转变为可变数组

    dataArray = [dict[@"DataArray"]mutableCopy];

    

    _tableView.delegate = self;

    

    _tableView.dataSource = self;

    

    [self.view addSubview:_tableView];

    

    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 736-50, 414, 50)];

    toolBar.backgroundColor = [UIColor greenColor];

    UIBarButtonItem *trash = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(trash)];

    

    UIBarButtonItem *refresh = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh)];

    

    UIBarButtonItem *add = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];

    

    toolBar.items = @[trash,refresh,add];

   

    [self.view addSubview:toolBar];

}


/*

 1、设置编辑模式

 tableView setEditing 如果设为yes,表明tableView处于编辑模式,否则为非编辑模式

 2、编辑模式有两种状态

   ①删除状态

   ②插入状态

 */

-(void)trash{

    

    isInsert = NO;

    

//    获取到此时tableView的编辑模式

    BOOL kbool = !_tableView.isEditing;

    

//    通过setEditingtableView

    [_tableView setEditing:kbool animated:YES];

}


//tableView进入编辑模式,会调用这个方法来确定编辑模式的状态,如果不实现这个方法,默认就是删除状态

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

    if (isInsert ==YES) {

        return UITableViewCellEditingStyleInsert;

    }else{

        return UITableViewCellEditingStyleDelete;

    }

    

    return UITableViewCellEditingStyleInsert;

}

//确定点击的是何种状态会调用这个方法,实现这个方法会让cell自带一个右滑效果,删除一个cell会重新执行numberOfRowsInSection方法,从而让要删除的cell消失,不会重新执行cellForRowAtIndexPath方法

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

    

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        [dataArray removeObjectAtIndex:indexPath.row];

        

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

    }else if (editingStyle == UITableViewCellEditingStyleInsert){

      

        NSMutableDictionary *tempDict = [NSMutableDictionary dictionary ];

        

//        包装一条数据

        tempDict[@"name"] = @"小薇";

        tempDict[@"content"] = @"花好香";

        tempDict[@"icon"] = @"xiaowei";

//       将包装好的数据放到数据源数组

        [dataArray insertObject:tempDict atIndex:indexPath.row];

        

        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];

    }

}


-(void)refresh{

    dataArray = [dict[@"DataArray"]mutableCopy];

    

//    reloadDatatableView重新加载

    [_tableView reloadData];

    

}


-(void)add{

    isInsert = YES;

    

//    tableView在编辑和非编辑状态切换

    //    获取到此时tableView的编辑模式

    BOOL kbool = !_tableView.isEditing;

    //    通过setEditingtableView

    [_tableView setEditing:kbool animated:YES];

}


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

    

    return dataArray.count;

}

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


    static NSString *cellId = @"cellId";

//    tableView里查找是不是有@"cellId"这个标记的cell

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];

    }

   

    cell.textLabel.text = dataArray[indexPath.row][@"name"];

    cell.detailTextLabel.text = dataArray[indexPath.row][@"content"];

    cell.backgroundColor = [UIColor greenColor];

    return cell;

}


 

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值