标签:coredata 数据库 通讯录 uitableviewcell uiimageview 对象
要求:将数据存放于CoreData数据库中,包括联系人的姓名和电话号码,将数据取出,显示在view上
详细代码如下
首先导入#import<CoreData/CoreData.h>
ViewController.h 文件
static NSManagedObjectContext * _context; //上下文
@interface TXLViewController ()
{
UITableView * _tableView;
NSMutableArray * _array;
TXLTableViewCell * _cell;
}
@end
@implementation TXLViewController
- (void)viewDidLoad {
[superviewDidLoad];
[self_loadDatabase]; //加载数据库
[self_loadView]; //加载视图
}
#pragma mark - 加载数据库
- (void)_loadDatabase
{
//[self _createTable]; //建表,建表完成后注释掉
[self_query]; //查询所有联系人信息
for(Person * personin _array)
{
NSLog(@"%@,%ld",[personvalueForKey:@"name"],[[personvalueForKey:@"telephoneNumber"]integerValue]);
}
}
- (void) _query
{
//3. 初始化模型
NSManagedObjectModel * model=[NSManagedObjectModelmergedModelFromBundles:nil];
//2. 创建基础库协调器
NSPersistentStoreCoordinator * psc=[[NSPersistentStoreCoordinatoralloc]initWithManagedObjectModel:model];
//5. 获取沙盒路径
NSString * path=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)firstObject];
path=[path stringByAppendingPathComponent:@"TXL.sqlite"];
NSLog(@"%@",path);
//6. 拼接URL
NSURL * url=[[NSURLalloc]initFileURLWithPath:path];
//4.为协调器添加基础库路径和基础库类型
[psc addPersistentStoreWithType:NSSQLiteStoreTypeconfiguration:nilURL:url options:nilerror:nil];
//1.创建上下文
_context=[[NSManagedObjectContextalloc]init];
_context.persistentStoreCoordinator=psc;
//初始化查询请求
NSFetchRequest * request=[[NSFetchRequestalloc]initWithEntityName:@"Person"];
//执行查询语句
_array = (NSMutableArray *)[_contextexecuteFetchRequest:request error:nil];
//取数据
//从数据库中取到的数据是数组类型,数组中存放的是对象
}
#pragma mark - 创建表
- (void) _createTable
{
//3. 初始化模型
NSManagedObjectModel * model=[NSManagedObjectModelmergedModelFromBundles:nil];
//2. 创建基础库协调器
NSPersistentStoreCoordinator * psc=[[NSPersistentStoreCoordinatoralloc]initWithManagedObjectModel:model];
//5. 获取沙盒路径
NSString * path=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)firstObject];
path=[path stringByAppendingPathComponent:@"TXL.sqlite"];
NSLog(@"%@",path);
//6. 拼接URL
NSURL * url=[[NSURLalloc]initFileURLWithPath:path];
//4.为协调器添加基础库路径和基础库类型
[psc addPersistentStoreWithType:NSSQLiteStoreTypeconfiguration:nilURL:url options:nilerror:nil];
//1.创建上下文
_context=[[NSManagedObjectContextalloc]init];
_context.persistentStoreCoordinator=psc;
//7. 插入数据
Person * person=[NSEntityDescriptioninsertNewObjectForEntityForName:@"Person"inManagedObjectContext:_context];
person.name=@"小王";
person.telephoneNumber=@(1008);
NSError * error;
[_contextsave:&error];
}
#pragma mark - 加载视图
- (void) _loadView
{
self.view.backgroundColor=[UIColorlightGrayColor];
_tableView= [[UITableViewalloc]initWithFrame:self.view.boundsstyle:UITableViewStylePlain];
_tableView.rowHeight=90;
self.navigationItem.title=@"通讯录列表";
//自定义视图,添加联系人按钮
UIButton * addPersonButton=[[UIButtonalloc]initWithFrame:CGRectMake(375-60,20, 30, 30)];
[addPersonButton setBackgroundImage:[UIImageimageNamed:@"add_person"]forState:UIControlStateNormal];
//给addPersonButton添加点击事件
[addPersonButton addTarget:selfaction:@selector(pushVC)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * addPersonItem=[[UIBarButtonItemalloc]initWithCustomView:addPersonButton];
//添加到当前的navigationItem
self.navigationItem.leftBarButtonItem=addPersonItem;
//自定义视图,查看添加关注的人员列表
UIButton * lookButton=[[UIButtonalloc]initWithFrame:CGRectMake(375-60,20, 30, 30)];
[lookButton setBackgroundImage:[UIImageimageNamed:@"star_big"]forState:UIControlStateNormal];
//给lookButton添加点击事件
[lookButton addTarget:selfaction:@selector(pushStarVC)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * lookStarItem=[[UIBarButtonItemalloc]initWithCustomView:lookButton];
self.navigationItem.rightBarButtonItem=lookStarItem;
_tableView.delegate=self; //普通代理
_tableView.dataSource=self; //数据源代理
[self.viewaddSubview:_tableView];
}
#pragma mark - pushVC跳转到添加联系人页面
- (void) pushVC
{
AddPersonViewController * addP=[[AddPersonViewControlleralloc]init];
[self.navigationControllerpushViewController:addP animated:YES];
}
#pragma mark - pushStarVC跳转到查看关注列表页面
- (void) pushStarVC
{
LookStarViewController * lookS=[[LookStarViewControlleralloc]init];
[self.navigationControllerpushViewController:lookS animated:YES];
}
#pragma mark - UITableViewDataSource代理需实现的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//定义唯一标识
static NSString * identy=@"myTableView";
//获取cell
_cell=[tableViewdequeueReusableCellWithIdentifier:identy]; //判断缓冲池里是否有数据
if (_cell==nil) //缓冲池为空的话,创建cell
{
_cell=[[TXLTableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identy];
}
[_cellsetData:_array[indexPath.row]];
return _cell;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
}
@end
@interface TXLTableViewCell ()
{
UIImageView * _leftImage; //左边头像图片
UIView * _middleView; //中间(姓名+号码)
UIButton * _rightButton; //右边关注图片
UILabel * _label;
UILabel * _numLabel;
}
@end
@implementation TXLTableViewCell
- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self=[superinitWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
//增加子视图
[self_addSubview];
}
return self;
}
- (void) _addSubview
{
self.selectionStyle=UITableViewCellSelectionStyleNone;
//1.添加左边图片
_leftImage=[[UIImageViewalloc]initWithFrame:CGRectMake(0,5, 70,70)];
_leftImage.backgroundColor=[UIColorclearColor];
[selfaddSubview:_leftImage];
//2.添加中间号码
_middleView=[[UIViewalloc]initWithFrame:CGRectMake(90,5, 150,80)];
_middleView.backgroundColor=[UIColorwhiteColor];
//添加姓名label
_label=[[UILabelalloc]initWithFrame:CGRectMake(5,10, 60, 90-30)];
_label.backgroundColor=[UIColorwhiteColor];
[_middleView addSubview:_label];
//添加号码label
_numLabel=[[UILabelalloc]initWithFrame:CGRectMake(10+60,10, 160, 60)];
_numLabel.backgroundColor=[UIColorwhiteColor];
[_middleView addSubview:_numLabel];
[selfaddSubview:_middleView];
//3.添加右边关注图片
_rightButton=[[UIButtonalloc]initWithFrame:CGRectMake(375-60+5,25, 45, 45)];
_rightButton.backgroundColor=[UIColorwhiteColor];
[_rightButtonsetBackgroundImage:[UIImageimageNamed:@"no"]forState:UIControlStateNormal];
[selfaddSubview:_rightButton];
}
- (void)setData:(Person *) person{
_label.text=person.name;
_numLabel.text=[NSStringstringWithFormat:@"%li",[person.telephoneNumberintegerValue]];
_leftImage.image=[UIImageimageNamed:@"person"];
}
@end
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface Person :NSManagedObject
@property (nonatomic,retain) NSString * name;
@property (nonatomic,retain) NSNumber * telephoneNumber;
@end
Person.m文件#import "Person.h"
@implementation Person
@dynamic name;
@dynamic telephoneNumber;
@end
其他的push页面可自行设置。标签:coredata 数据库 通讯录 uitableviewcell uiimageview 对象
原文地址:http://blog.csdn.net/qq_27364431/article/details/45921011