数据持久化存储回顾
不是每个程序员都是天才
集合写入文件
NSDictionary *dic1=@{@"aaa":@"aaa",@"cccc":@"vvvv"};
NSDictionary *dic2=@{@"ddd":@"ddd"};
NSArray *arr=@[dic1,dic2];
NSString *path=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:@"arr.plist"];
NSLog(@"%@",path);
NSLog(@"增加 : %d",[arr writeToFile:path atomically:YES]);
集合读取
NSString *path=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:@"data.plist"];
NSData *newData=[NSData dataWithContentsOfFile:path];
NSLog(@"%@",newData);
NSString *newStr= [[NSString alloc] initWithData:newData encoding:NSUTF8StringEncoding];
NSLog(@"%@",newStr);
熟练就能很快的敲出来,就是要从低级一步步的来
tableView增删改
@interface RootTableViewController ()<PostValueDelegate>
{
//记录选中行的索引值
NSIndexPath *CurrentIndexPath;
}
@end
@implementation RootTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"106.jpg"]];
//导航栏右按钮
self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addItem)];
self.Students=[NSMutableArray arrayWithCapacity:3];
[self.Students addObject:@"zs"];
[self.Students addObject:@"ls"];
[self.Students addObject:@"ww"];
//单元格复用 唯一标识
[self.tableView registerClass:[UITableViewCell class]forCellReuseIdentifier:@"reuseIdentifier"];
}
-(void)addItem
{
UIAlertController *alertC=[UIAlertController alertControllerWithTitle:@"增加学生信息" message:@"输入姓名" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *alertA1=[UIAlertAction actionWithTitle:@"添加" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField *textName=alertC.textFields[0];
//将输入框里的值赋给UITableView里面
[self.Students addObject:textName.text];
[self.tableView reloadData];//刷新
NSLog(@"真正的操作对象");
}];
//在提示框添加输入框
[alertC addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder=@"输入姓名";
}];
[alertC addAction:alertA1];
[self presentViewController:alertC animated:YES completion:^{
}];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
//刷新数据
[self.tableView reloadData];
}
//5.实现协议方法
-(void)PostValue:(NSString *)UserName
{
//为集合指定索引位置元素赋值
self.Students[CurrentIndexPath.row]=UserName;
//刷新
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source 多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
#pragma mark 每个分区返回多少
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.Students.count;
}
#pragma
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
cell.textLabel.text=self.Students[indexPath.row];
return cell;
}
#pragma 把信息推到下一页
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
ViewController *vc=[[ViewController alloc] init];
vc.Name=self.Students[indexPath.row];
//指定代理
vc.delegate=self;
[self.navigationController pushViewController:vc animated:YES];
}
//修改
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
//删除某行
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the row from the data source
[self.Students removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert)
{
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
//移动moveRow
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
//找到指定位置元素
NSString *name=self.Students[fromIndexPath.row];
//删除找到的元素
[self.Students removeObject:name];
//插入删除集合元素到指定位置
[self.Students insertObject:name atIndex:toIndexPath.row];
NSLog(@"%@",self.Students);
}
//让没格可以移动
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}