//设置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];
}