一.SQLite数据库(手机端)
//添加fmdb这个第三方库:需要修改两个步骤:
1)添加 -fno-objc-arc
2)添加libsqlite3.dylib
-
(instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {//创建数据库表格 //指定数据可存储的路径(沙盒下) self.dbPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/user.db"]; //数据库文件的尾椎是 .db //创建表的sql语句 NSString *createSql = @"create table if not exists userInfo1(id integer primary key autoincrement,name varchar(256),age integer default 0,image blob)"; if (![self.fmd open]) { NSLog(@"打开失败"); } //让数据库开始创建一张表 BOOL isSuccess = [self.fmd executeUpdate:createSql]; if (isSuccess) { NSLog(@"创建成功-%@",self.dbPath); }else { NSLog(@"创建失败"); }
}
return self;
}
//懒加载
-(FMDatabase *)fmd
{
if (!_fmd) {
//如果这个数据库不存在,表示创建
//如果已经存在,那么表示打开数据库
_fmd = [[FMDatabase alloc] initWithPath:self.dbPath];
}
return _fmd;
}
//添加数据
-
(IBAction)addItem:(id)sender {
NSString *addSql = @"insert into userInfo1(name,age,image)values(?,?,?)";
NSString *name = @"张三";
NSNumber *age = @25;
UIImage *image = [UIImage imageNamed:@"00.png"];
//把image对象转换成NSData
NSData *imageData = UIImagePNGRepresentation(image);//开始插入数据
if([self.fmd executeUpdate:addSql,name,age,imageData])
{
NSLog(@"插入成功");
}else
{
NSLog(@"插入失败");
}
}
//删除数据
-
(IBAction)deleteItem:(id)sender {
NSString *sql = @"delete from userInfo1 where id = ?";
if([self.fmd executeUpdate:sql,@2])
{
NSLog(@"删除成功");
}
}
//修改数据
-
(IBAction)updateItem:(id)sender {
NSString *sql = @"update userInfo1 set name=?,age=? where id=?";
NSString *name = @"李四";
NSNumber *age = @30;if([self.fmd executeUpdate:sql,name,age,@3])
{
NSLog(@"修改成功");
}
}
//查询数据
-
(IBAction)selectData:(id)sender {
//
NSString *selectSql = @"select *from userInfo1";//返回的是查询结果的集合
FMResultSet *reslutSet = [self.fmd executeQuery:selectSql];
//遍历这个集合,取出里面的数据,并打印
while ([reslutSet next]) {NSString *name = [reslutSet stringForColumn:@"name"]; NSInteger ID= [reslutSet intForColumn:@"id"]; NSInteger age = [reslutSet intForColumn:@"age"]; NSData *imageData = [reslutSet dataForColumn:@"image"]; //根据一个二进制数据加载UIImage对象 UIImage *image = [UIImage imageWithData:imageData]; NSLog(@"name:%@ ID:%ld age:%ld image:%@",name,ID,age,image);
}
}
二.用数据库做界面,具有增,删,改的功能
三.上拉刷新、下拉加载
-
(void)viewDidLoad {
[super viewDidLoad];self.dataArray = [NSMutableArray array];
for (int i=0; i<30; i++) {
NSString *str = [NSString stringWithFormat:@"我是第%d行",i+1];[self.dataArray addObject:str];
}
//注册cell
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CellID"];//UITableViewController自带的下拉刷新效果
self.refreshControl = [[UIRefreshControl alloc] init];//添加下拉刷新的功能
[self.refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
FooterView *footView = [[FooterView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width,你 30)];
self.tableView.tableFooterView = footView;
}
-(void)refresh
{
NSLog(@"开始加载数据。。。。");
//模拟一个耗时的请求功能
[self performSelector:@selector(stopRefresh) withObject:nil afterDelay:5];
}
-(void)stopRefresh
{
NSLog(@"停止加载");
[self.refreshControl endRefreshing];
}
pragma mark - Table view data source
-
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
} -
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataArray.count;
} -
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID" forIndexPath:indexPath];cell.textLabel.text = self.dataArray[indexPath.row];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
pragma mark -UIScrollViewDelegate
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
//根据偏移量去判断该不该加载
if (scrollView.contentOffset.y+scrollView.frame.size.height-scrollView.contentSize.height>64) {
NSLog(@"开始加载");
FooterView *fView = (FooterView *)self.tableView.tableFooterView;
fView.myLabel.text = @"正在加载中";
[fView.activityView startAnimating];
}
[self performSelector:@selector(stop) withObject:nil afterDelay:4];
}
-(void)stop
{
for (int i=0; i<10; i++) {
NSString *str = @"我是新加载的内容";
[self.dataArray addObject:str];
}
[self.tableView reloadData];
FooterView *fView = (FooterView *)self.tableView.tableFooterView;
fView.myLabel.text = @"上拉加载";
[fView.activityView stopAnimating];
}