[数据存储之七]CoreData操作练习
来源:互联网 发布:女仆咖啡厅 网络剧 编辑:程序博客网 时间:2024/05/19 00:42
算是第一个功能全面些的例子吧,实现了利用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)上个图吧:
2 0
- [数据存储之七]CoreData操作练习
- 数据存储之---CoreData
- IOS_数据存储之CoreData
- ios之应用数据存储四:CoreData
- iOS数据存储之CoreData(一)
- iOS数据存储之CoreData封装MagicalRecord
- [数据存储]CoreData
- 数据存储 CoreData
- 【iOS开发】数据存储之coredata、sqlite、fmdb和sqlitepersistentobject
- 数据存储方案之-CoreData的简单使用
- ios开发-CoreData数据存储
- iOS本地存储2--CoreData存储数据
- coredata 进阶篇——IOS之分析网易新闻存储数据(CoreData的使用,增删改查)
- [数据存储之二]NSUserDefaults练习
- ios开发使用CoreData存储数据时,快速写下FetchRequest语句操作
- 数据存储之文件操作
- iOS CoreData -之删除数据
- oracle学习笔记之七 数据操作
- Principles of Network Applications
- 原型设计思维
- 从随机过程到马尔科夫链蒙特卡洛方法
- JavaSe基础(1)-- 基本概念
- map常用操作
- [数据存储之七]CoreData操作练习
- NYOJ-123-士兵杀敌(四)
- 文章标题
- Android 浏览器跳转App
- J - Assign the task(dfs序+线段树)J
- js 文本框键盘输入事件
- 程序员编写技术文档的新手指南
- HDOJ 2011 多项式求和
- Linux入门