Students basic information into the system
A. The main interface structures
- The main interface built in storyboard
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190522224734481.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczMzk4OA==,size_16,color_FFFFFF,t_70)
- The main interface to add navigation controller is used to push interface
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190522224850686.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczMzk4OA==,size_16,color_FFFFFF,t_70)
- The associated object
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190522224959746.png)
- Related events
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190522225057448.png)
II. On the package file operation class (SqliteOperation)
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190522225129412.png)
- 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
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190522225815700.png)
@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
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190522231342445.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczMzk4OA==,size_16,color_FFFFFF,t_70)
- Obey UIImagePickerController agent
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
- Two proxy method to achieve
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190522231558102.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczMzk4OA==,size_16,color_FFFFFF,t_70)
- Click the submit button, the data is encapsulated into a model and then model the data saved to the database
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190522231658638.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczMzk4OA==,size_16,color_FFFFFF,t_70)
- Click the show button to enter the display interface, display student information database
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190522231834942.png)
2.ShowViewController class (inherited from UIViewController)
- Define a variable attribute tableview
@property(nonatomic, strong) UITableView *tableView;
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190522233154870.png)
@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
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190522233222459.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczMzk4OA==,size_16,color_FFFFFF,t_70)
- Implementing a proxy method
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190522233346101.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczMzk4OA==,size_16,color_FFFFFF,t_70)
3. Customize the cell, ShowTableViewCell class (inherited from UITableViewCell)
- Custom follows xib
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190522232104367.png)
- Association class
![Here Insert Picture Description](https://img-blog.csdnimg.cn/2019052223213371.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczMzk4OA==,size_16,color_FFFFFF,t_70)
- Set cellID
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190522232210284.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzczMzk4OA==,size_16,color_FFFFFF,t_70)
- The associated object
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190522232306237.png)
- .h file, add a member variable attribute model
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190522232411407.png)
- Set the value of the model
![Here Insert Picture Description](https://img-blog.csdnimg.cn/20190522232435393.png)