归档,反归档,编码者,解码者(重要)



//设置tableView的editing属性,会是表进入编辑状态,他会使用indexPath请求每一行,看每一行的编辑状态。默认为delete
    _tableView.editing = YES;
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{

}



1.归档和反归档
 //反归档
     NSData *data = [NSData dataWithContentsOfFile:[self saveDataPath]];
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data];

//归档

1.1工程文件路径

 
  NSData *data = [NSKeyedArchiver archivedDataWithRootObject:MutableArray];
NSString *path = [[NSBundle mainBundle] pathForResource:@"presidents" ofType:@"plist"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    dataArray = [[NSKeyedUnarchiver unarchiveObjectWithData:data] retain];



1.2沙盒目录下文件路径

-(NSString*)getSaveDataPath
{
    NSArray* array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* docPath = [array objectAtIndex:0];
    NSLog(@"%@",docPath);
    return [docPath stringByAppendingPathComponent:@"new_president.plist"];
}


-(void)saveData
{
    [NSKeyedArchiver archiveRootObject:dataArray toFile:[self getSaveDataPath]];
}


2.编码者和解码者
#define KEY_OF_NAME   @"keyName"
#define KEY_OF_PHONE @"keyPhone"
//coder编码者,是在序列化的时候使用
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    //NSLog(@"encodeWithCoder");
    [aCoder encodeObject:self.name forKey:KEY_OF_NAME];
    [aCoder encodeObject:self.Phone forKey:KEY_OF_PHONE];
}
//Dcoder解码者,是在反序列化的时候使用
- (id)initWithCoder:(NSCoder *)aDecoder
{
    //NSLog(@"initWithCoder");
    self = [super init];
    if (self)
    {
        self.name = [aDecoder decodeObjectForKey:KEY_OF_NAME];
        self.Phone = [aDecoder decodeObjectForKey:KEY_OF_PHONE];
    }
    return self;
}


3.以动画方式向表插入数据
 //2.以数组的长度为行,目前只有一个区,inSection为0
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[MutableArray count] inSection:0 ];
        //3.把位置添加到数组中.
        NSArray *array = [NSArray arrayWithObjects:indexPath, nil];
        //1.动画方式插入数据,插入位置为Array  //4.这样就有了位置array
        [_tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationTop];


4.删除表格方法
//点击delete的代理方法返回indexPath位置 editingStyle  eiting类型
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

}


5.表的拖动
//编辑状态
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    //sourceIndexPath  源 (区,行)
    NSString *strKey = [[dicData allKeys] objectAtIndex:[sourceIndexPath section]];
    NSMutableArray *array = [dicData objectForKey:strKey];
    NSString *value = [array objectAtIndex:[sourceIndexPath row]];
    //destinationIndexPath  目标 (区,行)
    NSString *strKey2 = [[dicData allKeys] objectAtIndex:[destinationIndexPath section]];
    NSMutableArray *array2 = [dicData objectForKey:strKey2];
    int insert = [destinationIndexPath row];
    //先插入
    [array2 insertObject:value atIndex:insert];
    //后移除
    [array removeObjectAtIndex:[sourceIndexPath row]];
    
    [_tableView reloadData];
}










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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值