1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
| #import "ViewController.h"
#import "FMDB.h"
#import "detailViewController.h"
@interface ViewController ()
@end
@implementation ViewController
NSMutableArray *listofname;
@synthesize tag;
- (void)viewDidLoad
{
[super viewDidLoad];
listofname = [[NSMutableArray alloc]init];
UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 600)];
//used to define the database
NSString *docdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *dbpath = [docdir stringByAppendingPathComponent:@"db1.sqlite"];
FMDatabase *db = [FMDatabase databaseWithPath:dbpath];
[db open];
//open the database
if(![db open]){
NSLog(@"could not open db");
}
//drop the table everytime because if you do not do this
//the insert sentense will happen every time
[db executeUpdate:@"drop table exercise"];
[db executeUpdate:@"drop table poetries"];
[db executeUpdate:@"create table exercise(_id integer, content text, answer text)"];
[db executeUpdate:@"create table poetries(_id integer, title text, author text, age text, size integer, line1 text, line2 text, line3 text, line4 text, explain text)"];
[db executeUpdate:@"insert into poetries(_id,title,author,age,size,line1,line2,line3,line4,explain) values(1,'赋得古原草送别','白居易','唐','5','离离原上草','一岁一枯荣','野火烧不尽','春风吹又生','译文:茂盛的野草长在古原上的野草多么茂盛,每年枯萎又每年新生。熊熊野火不能将它烧尽,春风吹过它又重获生命。')"];
[db executeUpdate:@"insert into poetries(_id,title,author,age,size,line1,line2,line3,line4,explain) values(2,'春夜喜雨','杜甫','唐','5','好雨知时节','当春乃发生','随风潜入夜','润物细无声','译文:春雨知道适应季节,当万物萌发生长时,它伴随着春风,在夜晚偷偷地及时降临,滋润万物又细微无声。') "];
[db executeUpdate:@"insert into poetries(_id,title,author,age,size,line1,line2,line3,line4,explain) values(3,'悯农','李绅','唐','5','锄禾日当午','汗滴禾下土','谁知盘中餐','粒粒皆辛苦','译文:这首悯农诗,写出了农民劳动的艰辛和对浪费粮食的愤慨。在盛夏的正午,农民顶着火辣辣的太阳锄地,汗水淼淌滴在庄稼地里。可是谁又知道,碗中的每一粒饭都包含着农民的辛苦啊!' )"];
[db executeUpdate:@"insert into poetries(_id,title,author,age,size,line1,line2,line3,line4,explain) values(4,'静夜思','李白','唐','5','床前明月光','疑是地上霜','举头望明月','低头思故乡','译文:那透过窗户映照在床前的月光,起初以为是一层层的白霜。仰首看那空中的一轮明月,不由得低下头来沉思,愈加想念自己的故乡。')"];
//query the database
FMResultSet *rs = [db executeQuery:@"select * from poetries"];
while ([rs next]) {
//add the title column in to the source of the table view
[listofname addObject:[rs stringForColumn:@"title"]];
}
[db close];
[tableview setDelegate:self]; //add the source and delegate
[tableview setDataSource:self];
[self.view addSubview:tableview];
}
//define the action in the cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentifier = @"name";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
}
NSString *cellvalue = [listofname objectAtIndex:indexPath.row];
cell.textLabel.text = cellvalue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.userInteractionEnabled = YES;
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [listofname count]; //define the number of the row in tableview
}
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 0; //define the indent
}
//define the event when click the cell(very important)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int mark = indexPath.row; //pass the row number to the next controller using tag
detailViewController *dv = [[detailViewController alloc] init];
dv.tag = mark;
[self presentModalViewController:dv animated:YES];
}
- (void)dealloc
{
[listofname dealloc];
[super dealloc];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
|