项目功能
1. 记录自己创建的事件
(1) 可以保存在系统文件夹
(2) 也可以保存在自定义的文件夹
2. 创建自定义文件夹
实现方法
sqlite
1. 记事本表
(1) WID , title , note , time , FID
(2) 创建表
(3) 插入数据
(4) 删除数据
(5) 根据FID查询数据
(6) 更改数据
2. 文件夹表
(1) FID , title , time
(2) 创建表
(3) 插入数据
(4) 查询数据(All)
(5) 删除数据
代码实现
--- AppDelegate.m
1 #import "AppDelegate.h" 2 #import "MainListViewController.h" 3 #import "DataBaseHandle.h" 4 5 @interface AppDelegate () 6 7 @end 8 9 @implementation AppDelegate 10 11 12 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 13 14 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 15 16 17 self.window.backgroundColor = [UIColor whiteColor]; 18 19 [self.window makeKeyAndVisible]; 20 21 //创建表 22 DataBaseHandle *dataBase = [DataBaseHandle shareDataBaseHandle]; 23 [dataBase openDB]; 24 25 [dataBase creatWordNoteTable]; 26 27 [dataBase creatFileMessageTable]; 28 29 [dataBase closeDB]; 30 31 32 MainListViewController *mainListVC = [[MainListViewController alloc] init]; 33 34 35 UINavigationController *mainListNaviVC = [[UINavigationController alloc] initWithRootViewController:mainListVC]; 36 37 self.window.rootViewController = mainListNaviVC; 38 39 return YES; 40 }
---DataBaseHandle.h
#import <Foundation/Foundation.h> #import "WordNote.h" #import "FileMessage.h" @interface DataBaseHandle : NSObject //数据库单例 + (DataBaseHandle *)shareDataBaseHandle; //打开数据库 - (void)openDB; //关闭数据库 - (void)closeDB; //创建记事本表 - (void)creatWordNoteTable; //创建文件夹表 - (void)creatFileMessageTable; //向WoedNote表里边插入一个WoedNote类型的model - (void)insertIntoWordNoteWith:(WordNote *)model; //向FileMessage表里边插入一个FileMessage类型的model - (void)insertIntoFileMessageWith:(FileMessage *)model; //根据WID删除掉WordNote里边的某条数据 - (void)deleteFromWordNoteWithWID:(NSInteger)WID; //根据FID删除WordNote里边的一堆数据 - (void)deleteFromWordNoteWithFID:(NSInteger)FID; //根据FID删除FileMessage里边的一条数据 - (void)deleteFromFileMessageWithFID:(NSInteger)FID; //根据FID在WordNote表里边查询数据 - (NSMutableArray *)searchFromWordNoteWithFID:(NSInteger)FID; //查询FileMessage表里面所有的数据 - (NSMutableArray *)searchAllFromFileMessage;
---DataBaseHandle.m
1 #import "DataBaseHandle.h" 2 #import <sqlite3.h> 3 4 @interface DataBaseHandle () 5 6 @property (nonatomic, strong) NSString *dbPath; 7 8 @end 9 10 static DataBaseHandle *dataBase = nil; 11 12 @implementation DataBaseHandle 13 14 + (DataBaseHandle *)shareDataBaseHandle 15 { 16 if (dataBase == nil ) { 17 dataBase = [DataBaseHandle new]; 18 } 19 return dataBase; 20 } 21 22 - (NSString *)dbPath 23 { 24 if (!_dbPath) { 25 NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]; 26 _dbPath = [document stringByAppendingPathComponent:@"wordnote.sqlite"]; 27 } 28 return _dbPath; 29 } 30 31 static sqlite3 *db = nil; 32 33 - (void)openDB 34 { 35 int result = sqlite3_open(self.dbPath.UTF8String, &db); 36 37 if (result == SQLITE_OK) { 38 NSLog(@"打开成功"); 39 }else{ 40 NSLog(@"失败了"); 41 } 42 43 } 44 45 - (void)closeDB 46 { 47 int result = sqlite3_close(db); 48 if (result == SQLITE_OK) { 49 NSLog(@"关闭成功"); 50 }else{ 51 NSLog(@"关不上"); 52 } 53 54 } 55 56 - (void)creatWordNoteTable 57 { 58 NSString *creatString = @"create table if not exists WordNote (WID integer primary key autoincrement,title text, note text, time text, FID integer)"; 59 int result = sqlite3_exec(db, creatString.UTF8String, NULL, NULL, NULL); 60 61 if (result == SQLITE_OK) { 62 NSLog(@"创表成功"); 63 }else 64 { 65 NSLog(@"创表失败"); 66 } 67 } 68 69 - (void)creatFileMessageTable 70 { 71 NSString *creatString = @"create table if not exists FileMessage (FID integer primary key autoincrement ,title text, time text)"; 72 73 int result = sqlite3_exec(db, creatString.UTF8String, NULL, NULL, NULL); 74 75 if (result == SQLITE_OK) { 76 NSLog(@"创建成功"); 77 }else 78 { 79 NSLog(@"创你妹"); 80 } 81 } 82 83 - (void)insertIntoWordNoteWith:(WordNote *)model 84 { 85 //插入流程 86 // 第一步:创建一个SQL语句 insert into 表名 (字段) values (多少个字段要有多少个?) 87 NSString *insertString = @"insert into WordNote (title, note, time, FID) values (?, ?, ?, ?)"; 88 // 第二步 : 创建一个伴随指针 89 sqlite3_stmt *stmt = nil; 90 // 第三步 : 使用sqlite3_prepare方法预执行SQL语句 91 int result = sqlite3_prepare(db, insertString.UTF8String, -1, &stmt, NULL); 92 // 第四步 : 判断是否预执行成功,如果成功了 进行下一步 93 if (result == SQLITE_OK) { 94 // 第五步 : 向?的位置 插入数据 95 sqlite3_bind_text(stmt, 1, model.title.UTF8String, -1, NULL); 96 sqlite3_bind_text(stmt, 2, model.note.UTF8String, -1, NULL); 97 sqlite3_bind_text(stmt, 3, model.time.UTF8String, -1, NULL); 98 sqlite3_bind_int64(stmt, 4, model.FID); 99 // 第六步 : 判断伴随指针是否完成 100 if (sqlite3_step(stmt) == SQLITE_DONE) { 101 NSLog(@"插入成功"); 102 } 103 }else 104 { 105 106 NSLog(@"错误信息 result== %d",result); 107 108 } 109 110 //第七步: 释放伴随指针 111 sqlite3_finalize(stmt); 112 } 113 114 - (void)insertIntoFileMessageWith:(FileMessage *)model 115 { 116 NSString *insertString = @"insert into FoleMessage (title, time) values (?, ?)"; 117 118 sqlite3_stmt *stmt = nil; 119 120 int result = sqlite3_prepare(db, insertString.UTF8String, -1, &stmt, NULL); 121 122 if (result == SQLITE_OK) { 123 sqlite3_bind_text(stmt, 1, model.title.UTF8String, -1, NULL); 124 sqlite3_bind_text(stmt, 2, model.time.UTF8String, -1, NULL); 125 if (sqlite3_step(stmt) == SQLITE_DONE) { 126 NSLog(@"插入成功"); 127 } 128 } 129 sqlite3_finalize(stmt); 130 } 131 132 #pragma mark -- 删除方法 133 134 - (void)deleteFromWordNoteWithWID:(NSInteger)WID 135 { 136 NSString *deleteString = [NSString stringWithFormat:@"delete from WordNote where WID = %ld", WID]; 137 138 int result = sqlite3_exec(db, deleteString.UTF8String, NULL, NULL, NULL); 139 if (result == SQLITE_OK) { 140 NSLog(@"删除成功"); 141 }else{ 142 NSLog(@"删除失败"); 143 } 144 145 } 146 147 - (void)deleteFromWordNoteWithFID:(NSInteger)FID 148 { 149 NSString *deleteString = [NSString stringWithFormat:@"delete from WordNote where FID = %ld", FID]; 150 151 int result = sqlite3_exec(db, deleteString.UTF8String, NULL, NULL, NULL); 152 if (result == SQLITE_OK) { 153 NSLog(@"删除成功"); 154 }else{ 155 NSLog(@"删除失败"); 156 } 157 } 158 159 - (void)deleteFromFileMessageWithFID:(NSInteger)FID 160 { 161 NSString *deleteString = [NSString stringWithFormat:@"delete from FileMessage where FID = %ld", FID]; 162 163 int result = sqlite3_exec(db, deleteString.UTF8String, NULL, NULL, NULL); 164 if (result == SQLITE_OK) { 165 NSLog(@"删除成功"); 166 }else{ 167 NSLog(@"删除失败"); 168 } 169 } 170 171 - (NSMutableArray *)searchFromWordNoteWithFID:(NSInteger)FID 172 { 173 //查询步骤 174 //第一步: 创建SQL语句 175 NSString *searchString = @"select * from WordNote where FID = ? "; 176 //第二步: 创建伴随指针 177 sqlite3_stmt *stmt = nil; 178 //第三步: 让查询语句 预执行 179 int result = sqlite3_prepare(db, searchString.UTF8String, -1, &stmt, NULL); 180 181 NSMutableArray *resultArray = [NSMutableArray array]; 182 183 //第四步: 判断预执行结果 184 if (result == SQLITE_OK) { 185 //第五步: 如果查询语句里面有 ? 需要一步绑定的过程 186 sqlite3_bind_int64(stmt, 1, FID); 187 //第六步: 循环取值 188 while (sqlite3_step(stmt) == SQLITE_ROW) { 189 WordNote *model = [WordNote new]; 190 191 model.WID = sqlite3_column_int(stmt, 0); 192 model.title = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 1)]; 193 model.note = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 2)]; 194 model.time = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 3)]; 195 model.FID = sqlite3_column_int(stmt, 4); 196 [resultArray addObject:model]; 197 } 198 } 199 sqlite3_finalize(stmt); 200 return resultArray; 201 } 202 203 - (NSMutableArray *)searchAllFromFileMessage 204 { 205 NSString *searchString = @"select * from FileMessage "; 206 207 sqlite3_stmt *stmt = nil; 208 209 int result = sqlite3_prepare(db, searchString.UTF8String, -1, &stmt, NULL); 210 211 NSMutableArray *searchArray = [NSMutableArray array]; 212 213 if (result == SQLITE_OK) { 214 while (sqlite3_step(stmt) == SQLITE_ROW) { 215 FileMessage *model = [FileMessage new]; 216 217 model.title = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)]; 218 model.time = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 2)]; 219 model.FID = sqlite3_column_int(stmt, 3); 220 [searchArray addObject:model]; 221 } 222 } 223 sqlite3_finalize(stmt); 224 return searchArray; 225 } 226 227 @end
----MainListViewController.m
#import "MainListViewController.h" #import "MainListOneLabelCollectionViewCell.h" #import "MainListTimeLabelCollectionViewCell.h" #import "MainListCollectionReusableView.h" #import "NoteViewController.h" #import "AddFileViewController.h" #import "DataBaseHandle.h" #import "MyLongPressGestureRecognizer.h" @interface MainListViewController ()<UICollectionViewDataSource, UICollectionViewDelegate> @property (nonatomic, strong) UICollectionView *collectionView; @end @implementation MainListViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)loadData { [super loadData]; DataBaseHandle *dataBaseHandle = [DataBaseHandle sharedDataBaseHandle]; [dataBaseHandle openDB]; self.dataArray = [dataBaseHandle searchAllFromFileMessage]; [self.collectionView reloadData]; } - (void)createView { [super createView]; UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.itemSize = CGSizeMake(90, 90); flowLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 20); flowLayout.minimumInteritemSpacing = 10; flowLayout.minimumLineSpacing = 10; flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout]; self.collectionView.backgroundColor = [UIColor whiteColor]; self.collectionView.delegate = self; self.collectionView.dataSource = self; [self.view addSubview:self.collectionView]; [self.collectionView registerClass:[MainListOneLabelCollectionViewCell class] forCellWithReuseIdentifier:@"oneLabelCELL"]; [self.collectionView registerClass:[MainListOneLabelCollectionViewCell class] forCellWithReuseIdentifier:@"addCELL"]; [self.collectionView registerClass:[MainListTimeLabelCollectionViewCell class] forCellWithReuseIdentifier:@"timeLabelCELL"]; [self.collectionView registerClass:[MainListCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"]; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 2; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { switch (section) { case 0: return 1; break; case 1: return self.dataArray.count + 1; break; default: return 1000; break; } } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { switch (indexPath.section) { case 0:{ MainListOneLabelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"oneLabelCELL" forIndexPath:indexPath]; return cell; }break; case 1:{ switch (indexPath.row) { case 0:{ MainListOneLabelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"addCELL" forIndexPath:indexPath]; cell.titleLabel.text = @"+"; cell.titleLabel.font = [UIFont systemFontOfSize:50]; return cell; }break; default:{ MainListTimeLabelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"timeLabelCELL" forIndexPath:indexPath]; FileMessage *message = self.dataArray[indexPath.row - 1]; [cell bindModel:message]; // 给cell添加一个长按手势 MyLongPressGestureRecognizer *longPG = [[MyLongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPGAction:)]; longPG.indexPath = indexPath; [cell addGestureRecognizer:longPG]; return cell; }break; } }break; default:{ UICollectionViewCell *cell = nil; return cell; }break; } } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { MainListCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath]; switch (indexPath.section) { case 0: reusableView.titleLabel.text = @"系统文件夹"; break; case 1: reusableView.titleLabel.text = @"我的文件夹"; break; default: break; } return reusableView; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { switch (indexPath.section) { case 0:{ NoteViewController *noteVC = [[NoteViewController alloc] init]; [self.navigationController pushViewController:noteVC animated:YES]; }break; case 1:{ switch (indexPath.row) { case 0:{ AddFileViewController *addFileVC = [[AddFileViewController alloc] init]; typeof(self) pSelf = self; addFileVC.MyBlock = ^(){ [pSelf loadData]; }; [self.navigationController pushViewController:addFileVC animated:YES]; }break; default:{ NoteViewController *noteVC = [[NoteViewController alloc] init]; FileMessage *message = self.dataArray[indexPath.row - 1]; noteVC.FID = message.FID; [self.navigationController pushViewController:noteVC animated:YES]; } break; } } default: break; } } // 长按收拾的触发方法 - (void)longPGAction:(MyLongPressGestureRecognizer *)longPG { UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"是否删除这个文件夹" message:nil preferredStyle:UIAlertControllerStyleAlert]; typeof(self) pSelf = self; UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { DataBaseHandle *dataBase = [DataBaseHandle sharedDataBaseHandle]; [dataBase openDB]; FileMessage *fileMessage = pSelf.dataArray[longPG.indexPath.row - 1]; [dataBase deleteFromFileMessageWithFID:fileMessage.FID]; [dataBase deleteFromWordNoteWithFID:fileMessage.FID]; [pSelf loadData]; }]; UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [controller addAction:action]; [controller addAction:action1]; [self presentViewController:controller animated:YES completion:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
-----MainListOneLabelCollectionViewCell.h
#import <UIKit/UIKit.h> @interface MainListOneLabelCollectionViewCell : UICollectionViewCell @property (nonatomic, strong) UILabel *titleLabel; @end
----MainListOneLabelCollectionViewCell.m
#import "MainListOneLabelCollectionViewCell.h"
@interface MainListOneLabelCollectionViewCell()
@end
@implementation MainListOneLabelCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.titleLabel = [[UILabel alloc] init];
self.titleLabel.text = @"系统文件夹";
self.titleLabel.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:self.titleLabel];
self.backgroundColor = [UIColor greenColor];
self.layer.cornerRadius = 10;
self.layer.borderWidth = 0.5;
self.layer.borderColor = [UIColor darkGrayColor].CGColor;
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.titleLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}
@end
-----MainListTimeLabelCollectionViewCell.h
#import <UIKit/UIKit.h> #import "FileMessage.h" @interface MainListTimeLabelCollectionViewCell : UICollectionViewCell - (void)bindModel:(FileMessage *)model; @end
-----MainListTimeLabelCollectionViewCell.m
#import "MainListTimeLabelCollectionViewCell.h" @interface MainListTimeLabelCollectionViewCell() @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UILabel *timeLabel; @end @implementation MainListTimeLabelCollectionViewCell - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.titleLabel = [[UILabel alloc] init]; [self.contentView addSubview:self.titleLabel]; self.titleLabel.text = @"我的文件夹"; self.backgroundColor = [UIColor greenColor]; self.titleLabel.textAlignment = NSTextAlignmentCenter; self.timeLabel = [[UILabel alloc] init]; [self.contentView addSubview:self.timeLabel]; self.timeLabel.font = [UIFont systemFontOfSize:10]; self.timeLabel.text = @"2016-03-28"; self.timeLabel.textAlignment = NSTextAlignmentCenter; self.layer.cornerRadius = 10; self.layer.borderWidth = 0.5; self.layer.borderColor = [UIColor darkGrayColor].CGColor; } return self; } - (void)layoutSubviews { [super layoutSubviews]; self.titleLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height / 2); self.timeLabel.frame = CGRectMake(0, self.frame.size.height / 2, self.frame.size.width, self.frame.size.height / 2); } @end
--------MainListCollectionReusableView.h
#import <UIKit/UIKit.h> @interface MainListCollectionReusableView : UICollectionReusableView @property (nonatomic, strong) UILabel *titleLabel; @end
--------MainListCollectionReusableView.m
#import "MainListCollectionReusableView.h" @implementation MainListCollectionReusableView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.titleLabel = [[UILabel alloc] init]; self.titleLabel.textAlignment = NSTextAlignmentLeft; self.titleLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5]; [self addSubview:self.titleLabel]; } return self; } - (void)layoutSubviews { [super layoutSubviews]; self.titleLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height); } @end
---------NoteViewController.h
#import "BaseViewController.h" @interface NoteViewController : BaseViewController @property (nonatomic, assign) NSInteger FID; @end
---------NoteViewController.m
#import "NoteViewController.h" #import "NoteListTableViewCell.h" #import "AddNoteViewController.h" #import "DataBaseHandle.h" #import "WordNote.h" @interface NoteViewController ()<UITableViewDataSource, UITableViewDelegate> @property (nonatomic, strong) UITableView *tableView; @end @implementation NoteViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)loadData { [super loadData]; DataBaseHandle *dataBaseHandle = [DataBaseHandle shareDataBaseHandle]; [dataBaseHandle openDB]; self.dataArray = [dataBaseHandle searchFromWordNoteWithFID:self.FID]; [self.tableView reloadData]; } - (void)createView{ [super createView]; self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; [self.tableView registerClass:[NoteListTableViewCell class] forCellReuseIdentifier:@"CELL"]; UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(barButtonItemClicked)]; self.navigationItem.rightBarButtonItem = barButtonItem; } - (void)barButtonItemClicked { AddNoteViewController *addNoteVC = [[AddNoteViewController alloc] init]; [self.navigationController pushViewController:addNoteVC animated:YES]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NoteListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath]; WordNote *model = self.dataArray[indexPath.row]; [cell bindModel:model]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { AddNoteViewController *addNoteVC = [[AddNoteViewController alloc] init];
addNoteVC.FID = self.FID;
addNoteVC.view.backgroundColor = [UIColor brownColor];
WordNote *model = self.dataArray[indexPath.row];
[addNoteVC bindModel:model];
[self.navigationController pushViewController:addNoteVC animated:YES];
}
@end
-----NoteListTableViewCell.h
#import <UIKit/UIKit.h> #import "WordNote.h" @interface NoteListTableViewCell : UITableViewCell - (void)bindModel:(WordNote *)model; @end
-----NoteListTableViewCell.m
#import "NoteListTableViewCell.h" @interface NoteListTableViewCell () @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UILabel *timeLabel; @end @implementation NoteListTableViewCell -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.titleLabel = [[UILabel alloc] init]; [self.contentView addSubview:self.titleLabel]; self.titleLabel.text = @"今天的事情"; self.titleLabel.textAlignment = NSTextAlignmentLeft; self.timeLabel = [[UILabel alloc] init]; self.timeLabel.text = @"2016-03-28"; [self.contentView addSubview:self.timeLabel]; } return self; } - (void)bindModel:(WordNote *)model { self.titleLabel.text = model.title; self.timeLabel.text = model.time; } - (void)layoutSubviews { [super layoutSubviews]; self.titleLabel.frame = CGRectMake(0, 0, self.frame.size.width / 3 * 2, self.frame.size.height); self.timeLabel.frame = CGRectMake(self.frame.size.width / 3 * 2, 0, self.frame.size.width / 3, self.frame.size.height); } - (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end
-------AddNoteViewController.h
#import "BaseViewController.h"
#import "WordNote.h"
typedef void(^block)();
@interface AddNoteViewController : BaseViewController
@property (nonatomic, copy) block myBlock;
@property (nonatomic, assign) NSInteger FID;
- (void)bindModel:(WordNote *)model;
@end
-------AddNoteViewController.m
#import "AddNoteViewController.h"
#import "DataBaseHandle.h"
@interface AddNoteViewController ()
@property (nonatomic, strong)UITextField *titleTextField;
@property (nonatomic, strong) UITextView *textView;
@end
@implementation AddNoteViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)createView
{
self.view.backgroundColor = [UIColor brownColor];
self.titleTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 74, self.view.frame.size.width - 20, 30)];
self.titleTextField.backgroundColor = [UIColor whiteColor];
self.titleTextField.placeholder = @"请输入标题";
self.titleTextField.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.titleTextField];
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 114, self.view.frame.size.width - 20, self.view.frame.size.height - 124)];
[self.view addSubview:self.textView];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(barButtonItemClicked)];
self.navigationItem.rightBarButtonItem = barButtonItem;
}
- (void)barButtonItemClicked
{
DataBaseHandle *dataBaseHandle = [DataBaseHandle sharedDataBaseHandle];
[dataBaseHandle openDB];
WordNote *wordNote = [[WordNote alloc] init];
wordNote.title = self.titleTextField.text;
wordNote.note = self.textView.text;
wordNote.FID = self.FID;
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd"];
wordNote.time = [formatter stringFromDate:date];
[dataBaseHandle insertIntoWordNoteWith:wordNote];
[dataBaseHandle closeDB];
self.myBlock();
[self.navigationController popViewControllerAnimated:YES];
}
- (void)bindModel:(WordNote *)model
{
self.titleTextField.text = model.title;
self.textView.text = model.note;
// 不让用户编辑了,所以关闭用户交互
self.titleTextField.userInteractionEnabled = NO;
self.textView.userInteractionEnabled = NO;
// 不需要保存,所以右边的置为nil
self.navigationItem.rightBarButtonItem = nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
-------AddFileViewController.h
#import "BaseViewController.h" // 使用block回调 让上一个页面刷新 typedef void(^block)(); @interface AddFileViewController : BaseViewController @property (nonatomic, copy) block MyBlock; @end
-------AddFileViewController.m
#import "AddFileViewController.h"
#import "DataBaseHandle.h"
#import "FileMessage.h"
@interface AddFileViewController ()
@property (nonatomic, strong)UITextField *titleTextField;
@end
@implementation AddFileViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)createView
{
self.view.backgroundColor = [UIColor blackColor];
self.titleTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 74, self.view.frame.size.width - 20, 30)];
self.titleTextField.textAlignment = NSTextAlignmentCenter;
self.titleTextField.backgroundColor = [UIColor whiteColor];
self.titleTextField.placeholder = @"请输入标题";
[self.view addSubview:self.titleTextField];
// 创建右边的barbuttonitem
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(barButtonItemClicked)];
self.navigationItem.rightBarButtonItem = barButtonItem;
}
- (void)barButtonItemClicked
{
// 存储数据
DataBaseHandle *dataBaseHandle = [DataBaseHandle sharedDataBaseHandle];
[dataBaseHandle openDB];
FileMessage *fileMessage = [[FileMessage alloc] init];
fileMessage.title = self.titleTextField.text;
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd"];
fileMessage.time = [formatter stringFromDate:date];
[dataBaseHandle insertIntoFileMessageWith:fileMessage];
// 触发自己的block
self.MyBlock();
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
------WordNote.h
#import <Foundation/Foundation.h> @interface WordNote : NSObject //记事本 @property(nonatomic, assign) NSInteger WID; //记事本Title @property(nonatomic, strong) NSString *title; //记事本记录的内容 @property(nonatomic, strong) NSString *note; //记录的时间 @property(nonatomic, strong) NSString *time; //文件夹的ID @property(nonatomic, assign) NSInteger FID; @end
----FileMessage.h
#import <Foundation/Foundation.h> @interface FileMessage : NSObject @property (nonatomic, assign) NSInteger FID; @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *time; @end