创建NoteListTableViewCell
NoteListTableViewCell.h中
引入数据库 #import "WordNote.h"
添加方法
- (void)bindModel:(WordNote *)model;
NoteListTableViewCell.m中
添加控件
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *timeLabel;
//初始化,并调用父类方法
-(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);
}
//创建AddNoteViewController
AddNoteViewController.h中
#warning 这里的引入从.m文件换到.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 "DataBaseHandle.h"
添加视图控件
@property (nonatomic, strong)UITextField *titleTextField;
@property (nonatomic, strong) UITextView *textView;
//调用方法并添加点击事件
- (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 insertToWordNoteWith:wordNote];
[dataBaseHandle closeDB];
[self.navigationController popViewControllerAnimated:YES];
}
#warning 这里添加了新代码
- (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;
}
创建 AddFileViewController
AddFileViewController.h中
#warning 这里添加的新的代码
// 使用block回调 让上一个页面刷新
typedef void(^block)();
@interface AddFileViewController : BaseViewController
@property (nonatomic, copy) block MyBlock;
@end
AddFileViewController.m中
@property (nonatomic, strong)UITextField *titleTextField;
- (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];
#warning 这里加了新代码
// 创建右边的barbuttonitem
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(barButtonItemClicked)];
self.navigationItem.rightBarButtonItem = barButtonItem;
}
- (void)barButtonItemClicked
{
#warning 这里添加了新代码
// 存储数据
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];
}