一、创建xxx.xcdatamodeld的可视化模型文件 eg:MyCoreData.xcdatamodeld
并且添加两个属性 name age
直接上代码:
import "ViewController.h"
import "AppDelegate.h"
import "FirstCoreData+CoreDataClass.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
/****** table ******/
@property (strong, nonatomic) UITableView *myTable;
/****** dataSourse ******/
@property (strong, nonatomic) NSMutableArray *dataSourse;
/****** appdelegate ******/
@property (strong, nonatomic) AppDelegate *myAppdelegate;
@end
@implementation ViewController
pragma mark - lazy
- (UITableView *)myTable{
if (!_myTable) {
_myTable = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
_myTable.delegate = self;
_myTable.dataSource = self;
_myTable.rowHeight = 50;
}
return _myTable;
} - (NSMutableArray *)dataSourse{
if (!_dataSourse) {
_dataSourse = [NSMutableArray array];
}
return _dataSourse;
}
pragma mark - 系统
-
(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//查询数据
//创建NSFetchRequest对象
NSFetchRequest *fetchRequest= [[NSFetchRequest alloc]initWithEntityName:@"FirstCoreData"];
//排序
NSSortDescriptor *sort = [[NSSortDescriptor alloc]initWithKey:@"age" ascending:YES];
fetchRequest.sortDescriptors = @[sort]; //这可以添加多种排序
NSError *error = nil;
NSArray *tempArray = [self.myAppdelegate.persistentContainer.viewContext executeFetchRequest:fetchRequest error:&error];if (error) {
NSLog(@"查询失败:%@",error);
}else{[self.dataSourse addObjectsFromArray:tempArray]; //添加到数据
}
}
-
(void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"coreDataTest";
UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addData)];
self.navigationItem.rightBarButtonItem = right;UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"长按修改" style:UIBarButtonItemStylePlain target:self action:@selector(change)];
self.navigationItem.leftBarButtonItem = left;[self.view addSubview:self.myTable];
self.myAppdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
}
pragma mark - addData
- (void)change{
}
- (void)addData{
//添加数据
NSEntityDescription *description = [NSEntityDescription entityForName:@"FirstCoreData" inManagedObjectContext:self.myAppdelegate.persistentContainer.viewContext];
FirstCoreData *first = [[FirstCoreData alloc]initWithEntity:description insertIntoManagedObjectContext:self.myAppdelegate.persistentContainer.viewContext];
first.name = @"ZhangSan";
int age = arc4random() % 30 + 1;
first.age = [NSString stringWithFormat:@"%d",age];
[self.dataSourse addObject:first]; //添加数据源
[self.myTable insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.dataSourse.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft]; //table插入数据
[self.myAppdelegate saveContext]; //保存数据
}
pragma mark - table delegate datasourse
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataSourse.count;
}-
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellID = [NSString stringWithFormat:@"%ld%ld",(long)indexPath.section,(long)indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
//获取数据
FirstCoreData *first = self.dataSourse[indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"name:%@ age:%@",first.name, first.age];
// 添加长按手势操作
UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(cellLongPress:)];
[cell addGestureRecognizer:longPressGesture];return cell;
}
//删除 -
(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//1.获取数据源
FirstCoreData *first = self.dataSourse[indexPath.row];
//2.删除数据数据源
[self.dataSourse removeObject:first];
//3.删除单元格
[self.myTable deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//4.删除数据管理器中的数据
[self.myAppdelegate.persistentContainer.viewContext deleteObject:first];
//5.保存
[self.myAppdelegate saveContext];}
} (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"删除";
}
// 长按修改-
(void)cellLongPress:(UILongPressGestureRecognizer *)tap{
CGPoint index = [tap locationInView:self.myTable];
NSIndexPath *indexPath = [self.myTable indexPathForRowAtPoint:index]; //获取行数
UITableViewCell *cell = (UITableViewCell *)tap.view; //强转cellFirstCoreData *first = self.dataSourse[indexPath.row];
first.name = @"修改后的闪闪发光";
cell.textLabel.textColor = [UIColor orangeColor];
[self.myTable reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];//保存数据
[self.myAppdelegate saveContext];
}