同UI进阶之CoreData1
//引入框架
#import<CoreData/CoreData.h>
//引入APPDelegate
#import "AppDelegate.h"
//引入model类
#import "LO_Class.h"
#import "LO_Student.h"
#import "LO_Teacher.h"
//声明属性
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSManagedObjectContext *objectContext;
@property (nonatomic, strong)NSMutableArray *dataArray;
- (void)viewDidLoad {
[super viewDidLoad];
self.dataArray = [NSMutableArray array];
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(barButtonItemClickde)];
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
self.objectContext = app.managedObjectContext;
[self seacherAll];
}
//实现方法
- (void)seacherAll
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"LO_Class" inManagedObjectContext:self.objectContext];
[fetchRequest setEntity:entity];
// Specify criteria for filtering which objects to fetch
// //使用谓词查询,里面类似sql查询语句中的where语句
// LO_Class *myClass = [LO_Class new];
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"studentClass = %@", myClass];
// [fetchRequest setPredicate:predicate];
// // Specify how the fetched objects should be sorted
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"number"ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
NSError *error = nil;
NSArray *fetchedObjects = [self.objectContext executeFetchRequest:fetchRequest error:&error];
if (error == nil) {
[self.dataArray setArray:fetchedObjects];
[self.tableView reloadData];
}
}
- (void)barButtonItemClickde{
//向Class表里面插入数据
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"LO_Class" inManagedObjectContext:self.objectContext];
LO_Class *myClass = [[LO_Class alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:self.objectContext];
int number = arc4random() % 100;
myClass.number = [NSNumber numberWithInt:number];
//向Teacher表里面插入数据
NSEntityDescription *teacherDescription = [NSEntityDescription entityForName:@"LO_Teacher" inManagedObjectContext:self.objectContext];
LO_Teacher *teacher = [[LO_Teacher alloc] initWithEntity:teacherDescription insertIntoManagedObjectContext:self.objectContext];
teacher.teacherName = @"张三";
teacher.teacherGender = @"男";
int age = arc4random() % 30 + 20;
teacher.teacherAge = [NSNumber numberWithInt:age];
//指定关联
myClass.myTeacher = teacher;
teacher.teacherClass = myClass;
int a = arc4random() % 50 + 30;
for (int i = 0; i < a; i++) {
NSEntityDescription *studentDescription = [NSEntityDescription entityForName:@"LO_Student" inManagedObjectContext:self.objectContext];
LO_Student *student = [[LO_Student alloc] initWithEntity:studentDescription insertIntoManagedObjectContext:self.objectContext];
int stuNumber = arc4random() % 50 + 30;
student.stuName = [NSString stringWithFormat:@"%d学号学生", stuNumber];
student.stuAge = [NSNumber numberWithInt:arc4random() % 10 + 20];
NSArray *array = @[@"男", @"女", @"王德良"];
int gender = arc4random() % array.count;
student.stuGender = array[gender];
student.studentClass = myClass;
student.studentTeacher = teacher;
//让班级和学生建立一对多的关系,多次进行添加
[myClass addMyStudentObject:student];
//让老师和学生建立一对多的关系,多次添加
[teacher addTeacherStudentObject:student];
}
NSError *error = nil;
[self.objectContext save:&error];
if (error == nil) {
[self.dataArray addObject:myClass];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.dataArray.count - 1 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else {
NSLog(@"插入成功");
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
LO_Class *myClass = self.dataArray[indexPath.row];
LO_Teacher *teacher = myClass.myTeacher;
NSLog(@"aaaa %@", myClass.number);
cell.textLabel.text = [NSString stringWithFormat:@"%@班级的%@老师",myClass.number, teacher.teacherName];
return cell;
}
网友评论