UItableView中编辑,删除,移动学习笔记

//
//  RootViewController.m
//  UI10TableViewController
//
//  Created by lanou3g on 15/8/20.
//  Copyright (c) 2015年 Yuxiang. All rights reserved.
//

#import "RootViewController.h"
#import "Person.h"
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView * tableView;
@property(nonatomic,retain)NSMutableDictionary * dataDictionary;
@property(nonatomic,retain)NSMutableArray * dataArray;
@property(nonatomic,assign)BOOL  isEdit;
@end


@implementation RootViewController

static NSString * identifier = @"My";

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    if(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]){
        NSString * path = [[NSBundle mainBundle]pathForResource:@"StudentInformation.plist" ofType:nil];
        //最外层字典
        _dataDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:path];
        //所有key值(分组名)
        _dataArray = [NSMutableArray arrayWithArray:[_dataDictionary allKeys]];
        //排序
        [_dataArray sortUsingSelector:@selector(compare:)];
        NSLog(@"%ld",_dataArray.count);
        //添加编辑按钮
        UIBarButtonItem * left= [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(edittableView:)];
        self.navigationItem.leftBarButtonItem = left;
        //添加编辑按钮
        //系统为我们提供一个编辑按钮,同时也提供了编辑方法,我们可以不用重写属性和方法
        self.navigationItem.rightBarButtonItem= self.editButtonItem;
        //默认显示edit
        self.editButtonItem.title = @"编辑";
        
    }
    return self;
}
#pragma mark -----tableView编辑
//(系统给的方法)
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
    
    _isEdit = NO;//左右按钮分开使用
    //向父类发送消息
    //在OC中,重写父类方法的时候,必须现象父类发送super消息,尤其是系统
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];//系统方法
    //换title
    self.navigationItem.rightBarButtonItem.title = editing?@"完成":@"编辑";
    
}

#pragma mark --------tableView编辑
//第一步:使用tableVIew处于编辑状态(自定义的)
-(void)edittableView:(UIBarButtonItem *)sender{
    _isEdit = YES;
    //当按钮显示编辑的时候,我破门操作这个按钮
    //1.tableView处于编辑状态
    //2.按钮先显示为"完成"
    //当按钮显示为完成状态时候,我们操作这个按钮
    //1.让tableView处于非编辑状态
    //2.按钮显示为"编辑"
    if([sender.title isEqualToString:@"编辑"]){
        sender.title = @"完成";
        [_tableView setEditing:YES animated:YES];
        sender.title = @"完成";
    }
    else{
        [_tableView setEditing:NO animated:YES];
        sender.title = @"编辑";
    }
}
//第二步:指定哪些路径可以被编辑

//-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        return NO;
    }
//    return YES;
//}
//第三步:指定编辑样式(默认返回删除)

//-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCellEditingStyleNone,
    UITableViewCellEditingStyleDelete,
    UITableViewCellEditingStyleInsert
//    
    if(indexPath.section < 3){
        return UITableViewCellEditingStyleDelete;
    }

    else{
    return UITableViewCellEditingStyleInsert;
    }
//    if(_isEdit == YES){
//        return UITableViewCellEditingStyleDelete;
//    }
//    else{
//        return UITableViewCellEditingStyleInsert;
//    }
//    
//}
//第四步:完成编辑
//- (void)tableView:(UITableView *)tableView commitEditingStyle:
//(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:
//(NSIndexPath *)indexPath{
//#warning -在进行删除数据的时候,我们要先删除数据,在删除UI
//    if(editingStyle == UITableViewCellEditingStyleDelete)
//    {
//        
//        NSLog(@"删除");
//        //1.操作数据
//        //(全组的人)
//        NSMutableArray * mutArray = [_dataDictionary objectForKey:_dataArray[indexPath.section]];
//        //当分组里面只有一个人的时候,我们删除完之后,需要删除其所在分组
//        if(mutArray.count == 1){
//            //移除整组
//            [_dataDictionary removeObjectForKey:_dataArray[indexPath.section]];
//            [_dataArray removeObjectAtIndex:indexPath.section];
//            //更新UI
//            NSIndexSet * set = [NSIndexSet indexSetWithIndex:indexPath.section];
//            //获取删除后的新UI
//            [_tableView deleteSections:set withRowAnimation:UITableViewRowAnimationTop];
//        }
//        else{
//        
//        //具体人
//        [mutArray removeObjectAtIndex:indexPath.row];
//        //2.操作UI
//        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
//        }
//        
//    }
//    else if (editingStyle == UITableViewCellEditingStyleInsert ){
//        NSLog(@"添加");
//        //创建字典
//        NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@"东东哥",@"name",@"男",@"gender",@"东东哥.png",@"image", nil];
//        //添加数据操作,(先找路径)
//        //找到对应的分区
//        NSMutableArray * array = [_dataDictionary objectForKey:_dataArray[indexPath.section]];
//        //加入数组中,+1是加载点击的联系人后面
//        [array insertObject:dic atIndex:indexPath.row + 1];
//        
//        //操作UI
//        //创建添加的位置
//        NSIndexPath * newPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
//        //插入
//        [_tableView insertRowsAtIndexPaths:@[newPath] withRowAnimation:UITableViewRowAnimationMiddle];
//    }
//}
#pragma mark --------以上是tableView编辑

#pragma mark --------以下是tableView移动
//第二个方法
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
//第三个方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:
(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath{
    
    //获取移动的数据(sourceIndexPath指原地址)
    NSMutableArray * array = [_dataDictionary objectForKey:_dataArray[sourceIndexPath.section]];
    
    NSDictionary * dic = [array objectAtIndex:sourceIndexPath.row];//复制原来数据
    
    //删除原数据
    [array removeObjectAtIndex:sourceIndexPath.row];
    //再插入
    [array insertObject:dic atIndex:destinationIndexPath.row];
    
}
//跨行移动
//检测跨行移动方法
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
    //如果在一个分区我们就可以让他随便移动
    if(sourceIndexPath.section == proposedDestinationIndexPath.section){
        return proposedDestinationIndexPath;
    }
    else{
        //否则,原路径回去
        return sourceIndexPath;
    }
    
}
#pragma mark --------以下是tableView移动
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //添加tableView
    [self addTableView];
    
    NSLog(@"%d",_isEdit);
}
#pragma mark ----tableView dataSouce
//多少分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArray.count;
}
//
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //取出当前分区的标题
    NSString * key = _dataArray[section];
    //取出当前分区的row的个数
    NSArray * array = _dataDictionary[key];
    return array.count;
}
//设置分区头
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return _dataArray[section];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //使用判断创建cell
/*   if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier;
    }*/
    //取出分区对应的数组
    NSArray * array = [_dataDictionary objectForKey:_dataArray[indexPath.section]];
    NSLog(@"82-------%@",array);
    //取出具体的字典(行)
    NSDictionary * dictionary = array[indexPath.row];
    NSLog(@"85-------%@",dictionary);
    //完成model赋值
    Person * per = [Person new];
    //kvc
    [per setValuesForKeysWithDictionary:dictionary];
    cell.textLabel.text = per.name;
    cell.imageView.image = [UIImage imageNamed:per.image];
    
    
    return cell;
}

-(void)addTableView{
    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 60;
    //注册,创建cell不在使用判断,直接可以使用iOS_6以后的新方法
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];
    [self.view addSubview:_tableView];
}

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return _dataArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值