算是第一个功能全面些的例子吧,实现了利用CoreData的添加,删除,修改,查询的操作。
功能简单,CoreData提供的东西也没仔细看。关键代码贴上:
添加和修改代码:
NSData* iconData = UIImageJPEGRepresentation(_ImgViewCurrent.image, 1.0); if (self.OpenViewModal == vmAddNew) { NSManagedObject* Record = [NSEntityDescription insertNewObjectForEntityForName:@"Students" inManagedObjectContext:_DBContext]; [Record setValue:_TextFiledName.text forKey:cdbName]; [Record setValue:_TextFiledPhone.text forKey:cdbTelephone]; [Record setValue:[NSNumber numberWithBool:_SwitchSex.on] forKey:cdbSex]; [Record setValue:iconData forKey:cdbIcon]; } else{ [self.EditReord setValue:_TextFiledName.text forKey:cdbName]; [self.EditReord setValue:_TextFiledName.text forKey:cdbName]; [self.EditReord setValue:_TextFiledPhone.text forKey:cdbTelephone]; [self.EditReord setValue:[NSNumber numberWithBool:_SwitchSex.on] forKey:cdbSex]; [self.EditReord setValue:iconData forKey:cdbIcon]; } [_DBContext save:nil]; [PublicFuns ShowMsg:@"保存完成!"]; [self.navigationController popViewControllerAnimated:YES];查询代码:
-(void)RefreshData { AppDelegate* delegate = [UIApplication sharedApplication].delegate; NSManagedObjectContext* _context = delegate.managedObjectContext; NSFetchRequest* Requst = [[NSFetchRequest alloc] initWithEntityName:@"Students"]; NSEntityDescription* Entity = [NSEntityDescription entityForName:@"Students" inManagedObjectContext:_context]; [Requst setEntity:Entity]; _FethchsObjects = [NSMutableArray arrayWithArray:[_context executeFetchRequest:Requst error:nil]]; [self.tableView reloadData]; }
NSManagedObject* Record = _FethchsObjects[indexPath.row]; UITableViewCell* Cell = nil; if ([[Record valueForKey:cdbSex] boolValue]) { Cell = [tableView dequeueReusableCellWithIdentifier:@"man"]; } else { Cell = [tableView dequeueReusableCellWithIdentifier:@"woman"]; } [Cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; _DoubleGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(EditStudent)]; [_DoubleGesture setNumberOfTapsRequired:2]; [Cell addGestureRecognizer:_DoubleGesture]; UIImageView* ImgView = (UIImageView*)[Cell viewWithTag:1]; UILabel* LabName = (UILabel*)[Cell viewWithTag:2]; UILabel* LabPhone = (UILabel*)[Cell viewWithTag:3]; NSData* iconData = (NSData*)[Record valueForKey:cdbIcon]; ImgView.image = [UIImage imageWithData:iconData]; LabName.text = [Record valueForKey:cdbName]; LabPhone.text = [Record valueForKey:cdbTelephone]; return Cell;删除代码:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger row = [indexPath row]; if (editingStyle == UITableViewCellEditingStyleDelete) { AppDelegate* delegate = [UIApplication sharedApplication].delegate; NSManagedObjectContext* _context = delegate.managedObjectContext; [_context deleteObject:_CurrentObject]; [_FethchsObjects removeObject:_CurrentObject]; [_context save:nil]; NSArray* DelArr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:row inSection:0]]; [tableView deleteRowsAtIndexPaths:DelArr withRowAnimation:UITableViewRowAnimationBottom]; [PublicFuns ShowMsg:@"删除完成!"]; } }总结下吧:
1,实现了利用CoreData,存储数据,例子中包括字符串,BOOL,二进制类型的数据。
2,操练了利用stroyBoard实现水平等分的方法,这个开始研究的时候,挺费时间的。
3,利用了oc的performSelector:WithObject的概念,给对象发送消息,降低耦合。但arc环境处理费点儿事,下面是两种处理方式。
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { id EditController = segue.destinationViewController; /*大师级写法啊~*/ SEL SelModal = NSSelectorFromString(@"setOpenViewModal:"); IMP impModal = [EditController methodForSelector:SelModal]; void(*FuncModal)(id,SEL,NSInteger) = (void*) impModal; FuncModal(EditController,SelModal,[sender integerValue]); /*SEL SelRecord = NSSelectorFromString(@"setEditReord:"); IMP impRcord = [EditController methodForSelector:SelRecord]; void(*FuncRecord)(id,SEL,id) = (void*) impRcord; FuncRecord(EditController,SelModal,_CurrentObject);*/ /*推荐方式啊,一劳永逸~*/ SuppressPerformSelectorLeakWarning ( SEL SelRecord = NSSelectorFromString(@"setEditReord:"); [EditController performSelector:SelRecord withObject:_CurrentObject]; ); }宏的原型:
#define SuppressPerformSelectorLeakWarning(Stuff) \ do { \ _Pragma("clang diagnostic push") \ _Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \ Stuff; \ _Pragma("clang diagnostic pop") \ } while (0)上个图吧: