CoreData 核心数据;在MVC中M是数据模型,平常我们使用的时候,都是创建一个继承于NSObject的Model,创建对应的属性;而CoreData是属于系统的一种数据管理系统
数据管理一般都是增删改查,我是直接使用storyBoard创建了一个表视图控制器,创建了一个表视图,将数据展示到表视图中,下面我将自己所写的代码贴出来,代码有些乱,但是都实现了功能:
#pragma mark 添加一个班级
- (IBAction)addClass:(id)sender {
//1.创建一个班级对象 (增删改查中的增)
//创建一个视图对象的描述 得到实体(表)是什么样的结构
NSEntityDescription *classED = [NSEntityDescriptionentityForName:@"Entity"inManagedObjectContext:self.context];
//接着创建一个班级对象,这个对象继承于NAManagerdObject;并且把这条数据保存到当前上下文(一个暂时的数据库)
Entity *class = [[Entityalloc]initWithEntity:classEDinsertIntoManagedObjectContext:self.context];
//赋值 使用UIImageJPEGRepresentation这个函数将图片转换成NSData
//图片赋值
class.headImage =UIImageJPEGRepresentation([UIImageimageNamed:@"8.jpg"],1.0);
int num = arc4random() %1000 +1000;
class.name = [NSStringstringWithFormat:@"XAS15 %d",num];
NSArray *cityArr =@[@"北京",@"上海",@"大连",@"郑州",@"广州",@"杭州",@"西安"];
int index = arc4random() %6;
class.city = cityArr[index];
class.createTime = [NSDatedate];
//2.将此保存到文件
NSError *error ;
if (!error) {
[self.contextsave:&error];
//3.添加到数据源
[self.datasourceaddObject:class];
//4.添加到UI界面
//生成最后一行的下标
NSIndexPath *indexPath = [NSIndexPathindexPathForRow:self.datasource.count -1 inSection:0];
//将数据添加到列表的最后一行
[self.tableViewinsertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
//让tableView滑动到添加到一行
[self.tableViewscrollToRowAtIndexPath:indexPathatScrollPosition:UITableViewScrollPositionMiddleanimated:YES];
}
}
#pragma mark ------------ 增删改查中的查
- (void)readData{
//创建请求对象
NSFetchRequest *request = [NSFetchRequestfetchRequestWithEntityName:@"CustomModel"];
//可以设置过滤条件是coreData中的一个属性
NSPredicate *pre = [NSPredicatepredicateWithFormat:@"city = %@",@"大连"];
request.predicate = pre;
NSSortDescriptor *sort = [NSSortDescriptorsortDescriptorWithKey:@"city"ascending:YES];
request.sortDescriptors = @[sort]; //这里是一个数组
NSArray *classes = [self.contextexecuteFetchRequest:requesterror:nil];
[self.datasourcesetArray:classes];
}
#pragma mark - ------- 修改
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//设置修改条件 (把大连的city修改成西安)
NSFetchRequest *request = [NSFetchRequestfetchRequestWithEntityName:@"Entity"];
NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"city = %@",@"郑州"];
request.predicate = predicate;
NSSortDescriptor *sortDescriptior = [NSSortDescriptorsortDescriptorWithKey:@"city"ascending:YES];
request.sortDescriptors = @[sortDescriptior];
NSArray *classes = [self.contextexecuteFetchRequest:requesterror:nil];
//查询完成之后开始修改
//遍历查询返回的数组
for (Entity *classin classes) {
class.city = @"西安";
}
NSError *error;
[self.contextsave:&error];
[self.tableViewreloadData];
}
由于表视图有自己的删除方法,所以我直接就在这个方法中进行数据的删除:
#pragma mark ----------- 表视图自带删除功能 所以在这个自带方法中进行删除操作
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle ==UITableViewCellEditingStyleDelete) {
//删除数据库中的数据
CustomModel *model = self.datasource[indexPath.row];
[self.contextdeleteObject:model];
//暂时数据源同步到本地数据源
NSError *error;
[self.contextsave:&error];
//移除数据源中的数据
if (!error) {
[self.datasourceremoveObject:model];
[tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationFade];
}
} elseif (editingStyle ==UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
这里涉及的只是coreData的一对一的数据管理,较为简单;请多多指教~