Students basic information into the system
A. The main interface structures
- The main interface built in storyboard
- The main interface to add navigation controller is used to push interface
- The associated object
- Related events
II. On the package file operation class (SqliteOperation)
- Singleton, an object of this class so that the current management object has always existed
+ (SqliteOperation *)sharedOperaion;
static SqliteOperation *instance = nil;
@implementation SqliteOperation
+ (SqliteOperation *)sharedOperaion{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [SqliteOperation new];
});
return instance;
}
@end
- Open a database, if you can open the table is created
- (BOOL)openSql;
- (BOOL)openSql{
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"stu.sql"];
NSLog(@"%@",path);
if (sqlite3_open([path UTF8String], &_sql) == SQLITE_OK) {
NSLog(@"打开数据库成功");
NSString *createTableSql = @"create table if not exists stu(id integer primary key autoincrement, name text, age text, icon blob)";
return [self executeSql:createTableSql];
}else{
return NO;
}
}
- (BOOL)executeSql:(NSString *)sql{
if(sqlite3_exec(_sql, sql.UTF8String, NULL, NULL, NULL) == SQLITE_OK){
NSLog(@"执行语句成功:%@",sql);
return YES;
}else{
NSLog(@"执行语句失败:%@",sql);
return NO;
}
}
- We should save data, all the information the students are first encapsulated into a model
@interface StudentModel : NSObject
@property(nonatomic, strong) UIImage *icon;
@property(nonatomic, strong) NSString *name;
@property(nonatomic, assign) NSString *age;
@end
- Write a function to save the student model, the model information stored in the database
- (void)save:(StudentModel *)model{
NSString *insertSql = @"insert into stu(name,age,icon) values(?,?,?)";
sqlite3_stmt *stmt = NULL;
if(sqlite3_prepare(_sql, insertSql.UTF8String, -1, &stmt, NULL) != SQLITE_OK){
NSLog(@"预处理失败");
return;
}
NSData *data = UIImagePNGRepresentation(model.icon);
sqlite3_bind_text(stmt, 1, model.name.UTF8String, -1, NULL);
sqlite3_bind_text(stmt, 2, model.age.UTF8String, -1, NULL);
sqlite3_bind_blob(stmt, 3, data.bytes, (int)data.length, NULL);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
- Write a loading function is used to read the student information database to display to the cell, the function returns an array of model
@property (nonatomic, strong) NSMutableArray *dataArray;
- (NSMutableArray *)dataArray{
if (_dataArray == nil) {
self.dataArray = [NSMutableArray array];
}
return _dataArray;
}
- (NSArray *)loadData{
NSString *selectSql = [NSString stringWithFormat:@"select * from stu limit %ld,2", self.dataArray.count];
sqlite3_stmt *stmt = NULL;
if(sqlite3_prepare(_sql, selectSql.UTF8String, -1, &stmt, NULL) != SQLITE_OK){
return nil;
}
while (sqlite3_step(stmt) == SQLITE_ROW) {
StudentModel *model = [StudentModel new];
model.name = [NSString stringWithCString:(const char *)sqlite3_column_text(stmt, 1) encoding:NSUTF8StringEncoding];
model.age = [NSString stringWithCString:(const char *)sqlite3_column_text(stmt, 2) encoding:NSUTF8StringEncoding];
NSData *imageData = [NSData dataWithBytes:sqlite3_column_blob(stmt, 3) length:sqlite3_column_bytes(stmt, 3)];
model.icon = [UIImage imageWithData:imageData];
[self.dataArray addObject:model];
}
return self.dataArray;
}
III. Information display categories and custom cell
1.ViewController class
- Setting information input box prompt text
_nameTextField.placeholder = @"Name";
_ageTextFeild.placeholder = @"Age";
- Click the + button to complete the reading of a photograph from the system function
- Obey UIImagePickerController agent
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
- Two proxy method to achieve
- Click the submit button, the data is encapsulated into a model and then model the data saved to the database
- Click the show button to enter the display interface, display student information database
2.ShowViewController class (inherited from UIViewController)
- Define a variable attribute tableview
@property(nonatomic, strong) UITableView *tableView;
@interface ShowViewController ()<UITableViewDataSource,UITableViewDelegate>
- Receiving model data read from the database using a data
@property(nonatomic, strong) NSArray *dataArray;
- Pull down to refresh, to read data from the database model
- Implementing a proxy method
3. Customize the cell, ShowTableViewCell class (inherited from UITableViewCell)
- Custom follows xib
- Association class
- Set cellID
- The associated object
- .h file, add a member variable attribute model
- Set the value of the model