//
// ViewController.m
// demo3
//
// Created by wusiping on 15/11/20.
// Copyright (c) 2015年 wusiping. All rights reserved.
//
未经允许,请勿转载
拷贝之后,即可运行
#import "ViewController.h"
#import <sqlite3.h>
#define numberOfButton 5
#define buttonW 100
#define buttonH 100
#define numberOfRowInTable 3
#define buttonRap 10
@interface ViewController ()<UITableViewDataSource>
{
sqlite3 *dataBase;
}
@property (nonatomic,weak)UIScrollView *myScrollView;
@property (nonatomic,weak)UITableView *myTableView;
@property (nonatomic,strong)NSMutableArray *searchResultArray;
@end
@implementation ViewController
//1.打开或创建数据库
- (void)openDateBase
{
NSString *files = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject ]stringByAppendingPathComponent:@"student.sqlite"];
int result = sqlite3_open(files.UTF8String, &(dataBase));
if (result == SQLITE_OK) {
NSLog( @"打开成功");
}else{
NSLog(@"打开失败");
}
double iosVersion = [[UIDevicecurrentDevice].systemVersiondoubleValue];
if (iosVersion < 8 ) {
NSLog(@"= 7");
}
}
//2.为数据库建表
- (void)createTableForSqlite
{
constchar *sql ="create table if not exists t_student (id integer primary key autoincrement ,name text);";
char *errorMsg;
int result = sqlite3_exec(dataBase, sql ,NULL, NULL, &errorMsg);
if (result == SQLITE_OK) {
NSLog(@"table创建成功");
}else{
NSLog(@"创表失败%s",errorMsg);
}
}
//3.将名字插入学生表
- (void)insetToSqlite
{
for (int i =0 ; i < 60 ; i ++) {
NSString *studentName = [NSStringstringWithFormat:@"jack%d",arc4random()%100];
constchar *sql =[NSString stringWithFormat:@"insert into t_student (name) values ('%@');",studentName].UTF8String;
char *errorMsg;
int result = sqlite3_exec(dataBase, sql , NULL, NULL, &errorMsg);
if (result == SQLITE_OK) {
NSLog(@"插入数据成功");
}else{
NSLog(@"插入数据失败%s",errorMsg);
}
}
}
//4.查询一张表,并将查询结果存入动态数组
- (void)search:(NSString *)sql
{
sqlite3_stmt *stmt = NULL;
int result = sqlite3_prepare_v2(dataBase, sql.UTF8String , -1, &stmt,NULL);
if (result == SQLITE_OK) {
NSLog(@"查询语句正确");
NSMutableArray *array = [[NSMutableArrayalloc]init];
while ( sqlite3_step(stmt) ==SQLITE_ROW) {
const unsigned char *sname = sqlite3_column_text(stmt, 0);
[array addObject:[NSStringstringWithUTF8String:sname]];
}
_searchResultArray = array;
}else{
NSLog(@"查询语句错误");
}
}
- (void)viewDidLoad {
[superviewDidLoad];
[selfopenDateBase];
[selfcreateTableForSqlite];
// [self insetToSqlite];
// Do any additional setup after loading the view, typically from a nib.
//1.添加一个scrollView
[selfsetUpMyScroll];
[selfsetUpMyTableView];
[selfaddButtonToMyScroolView];
}
//5.设置scrollView
- (void)setUpMyScroll
{
CGFloat myScrollX = 0;
CGFloat myScrollY = 0;
CGFloat myScrollW = [UIScreenmainScreen].bounds.size.width;
CGFloat myScrollH = [UIScreenmainScreen].bounds.size.height *0.5;
CGRect myScroollFrame = CGRectMake(myScrollX, myScrollY, myScrollW, myScrollH);
UIScrollView *myScroll = [[UIScrollViewalloc]init];
// [myScroll setBackgroundColor:[UIColor blackColor]];
myScroll.frame = myScroollFrame;
myScroll.contentSize = CGSizeMake((buttonW + buttonRap) *5, buttonH);
_myScrollView = myScroll;
[self.viewaddSubview:_myScrollView];
}
//6.将按钮添加到scrollview里面去
- (void)addButtonToMyScroolView
{
CGFloat buttonY = 200;
for (int i =0 ; i < numberOfButton ; i ++) {
UIButton *button = [[UIButtonalloc]init];
CGRect buttonFrame = CGRectMake(i * (buttonW + buttonRap), buttonY, buttonW, buttonH);
button.frame = buttonFrame;
NSString *buttontitle = [NSStringstringWithFormat:@"button%d",i ];
[button setTitle:buttontitleforState:UIControlStateNormal];
[button setBackgroundColor:[UIColorgrayColor]];
button.tag = i;
[button addTarget:selfaction:@selector(touchButton:)forControlEvents:UIControlEventTouchUpInside];
[_myScrollView addSubview:button];
}
}
- (void)touchButton:(UIButton *)btn
{
NSLog(@"点击了button%ld",(long)btn.tag);
long int b = btn.tag *numberOfRowInTable;
NSString *sql = [NSStringstringWithFormat:@"select name from t_student limit 15 offset %ld", b];
[self search:sql ];
[_myTableView reloadData];
}
- (void)setUpMyTableView
{
CGFloat myTableViewX = 0;
CGFloat myTableViewY = [UIScreenmainScreen].bounds.size.height *0.5;
CGFloat myTableViewW = [UIScreenmainScreen].bounds.size.width;
CGFloat myTableViewH = [UIScreenmainScreen].bounds.size.height *0.5;
CGRect myTableViewFrame = CGRectMake(myTableViewX, myTableViewY, myTableViewW, myTableViewH);
UITableView *myTableView = [[UITableViewalloc]init];
myTableView.frame = myTableViewFrame;
_myTableView = myTableView;
_myTableView.dataSource =self;
[self.viewaddSubview:_myTableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
returnnumberOfRowInTable;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cell"];
cell.textLabel.text =_searchResultArray[indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end