·使用数据库实现如下对用户的添加和修改,添加用户名,密码以及年龄,
1在ViewController.h文件中
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> {
__weak IBOutlet UITableView *_tableView;
NSMutableArray *_data;
}
2在ViewController.m文件中
#import "ViewController.h"
#import "AddViewController.h"
#import "User.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAc)];
self.navigationItem.leftBarButtonItem = button;
//注册通知
//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationChanged:) name:@"notification" object:nil];
}
- (void)addAc{
UIStoryboard *stror = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
AddViewController *add = [stror instantiateViewControllerWithIdentifier:@"iden"];
[self.navigationController pushViewController:add animated:YES];
[add completionBlock:^(NSArray *block) {
_data = [NSMutableArray arrayWithArray:block];
}];
}
#pragma mark - 接收通知事件
//- (void)notificationChanged:(NSNotification *)notifi{
//
// //接收传过来的数据
// _data = notifi.object;
//
//}
#pragma mark---UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *str = @"cell123";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
if ( cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
}
User *user = _data[indexPath.row];
cell.textLabel.text = user.username;
NSInteger age = [[NSString stringWithFormat:@"%@",user.age] integerValue];
NSNumber *num = @(age);
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",num];
//刷新指定的单元格
//[_tableView reloadRowsAtIndexPaths:@[@"indexPath.row"] withRowAnimation:UITableViewRowAnimationFade];
return cell;
}
@end
3在AddViewController.h文件中
#import <UIKit/UIKit.h>
typedef void(^completionBlock)(NSArray *block);
@interface AddViewController : UIViewController {
__weak IBOutlet UITextField *_username;
__weak IBOutlet UITextField *_password;
__weak IBOutlet UITextField *_age;
}
@property (weak, nonatomic) IBOutlet UIButton *_add;
@property(nonatomic,copy)completionBlock block;
@property (nonatomic, strong)NSMutableArray *modelArray;
- (void)completionBlock:(completionBlock)block;
@end
4在 AddViewController.m文件中
#import "AddViewController.h"
#import <CoreData/CoreData.h>
#import "User.h"
@interface AddViewController ()
@end
@implementation AddViewController {
NSManagedObjectContext *context;
NSArray *datas;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)_add:(UIButton *)sender {
//创建model模型对象
NSURL *url = [[NSBundle mainBundle] URLForResource:@"CoreDataModel" withExtension:@"momd"];
NSManagedObjectModel *dataModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:url];
//1创建协调对象
NSPersistentStoreCoordinator *coodinate = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:dataModel];
NSString *file = [NSHomeDirectory() stringByAppendingString:@"/Documents/dbase.sqlite"];
NSLog(@"file:%@",file);
NSURL *url2 = [NSURL fileURLWithPath:file];
//2协调对象管理数据库
[coodinate addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url2 options:nil error:nil];
//创建context对象
context = [[NSManagedObjectContext alloc] init];
//将协调对象交给context对象
[context setPersistentStoreCoordinator:coodinate];
//点击按钮的时候要插入数据
[self insert];
//插入完数据就要查询数据
[self fetch];
}
//添加一条数据
- (void)insert; {
User *user = [NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:context];
user.username = _username.text;
user.password = _password.text;
NSInteger age = [[NSString stringWithFormat:@"%@",_age.text] integerValue];
user.age = @(age);
//插入数据
[context insertObject:user];
//保存数据
BOOL success = [context save:nil];
if (success) {
NSLog(@"添加数据成功");
}
}
//查询所有数据
- (void)fetch {
//创建查询
NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"User"];
//查询
datas = [context executeFetchRequest:fetch error:nil];
//查询出来数据的时候就发送一个通知,将查询出来的数据传过去,在viewcontroller里接收通知
// [[NSNotificationCenter defaultCenter] postNotificationName:@"notification" object:datas];
//block回调
if (_block!=nil) {
_block(datas);
}
NSLog(@"datas:%@",datas);
}
//block
- (void)completionBlock:(completionBlock)block{
if (_block!=block) {
_block = [block copy];
// //block回调
// _block(datas);
}
}
5 User.h文件中
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface User : NSManagedObject
@property (nonatomic, retain) NSString * username;
@property (nonatomic, retain) NSString * password;
@property (nonatomic, retain) NSNumber * age;
@end
6 User.m文件中
#import "User.h"
@implementation User
@dynamic username;
@dynamic password;
@dynamic age;
@end