1.框架 我使用Realm來作為資料庫的框架,還有SDAutoLayout做適配。不會用的,也沒關係,這兩個框架簡單的很。 2.邏輯設置 日記記錄的時候就記錄三個數據,標題,內容,寫日記的時間。這個時間精確到秒,相當於資料庫的主鍵。我們點擊以前寫的日記項,也可以對其進行修改,這個時間也會修改。 3. ...
1.框架
我使用Realm來作為資料庫的框架,還有SDAutoLayout做適配。不會用的,也沒關係,這兩個框架簡單的很。
2.邏輯設置
日記記錄的時候就記錄三個數據,標題,內容,寫日記的時間。這個時間精確到秒,相當於資料庫的主鍵。我們點擊以前寫的日記項,也可以對其進行修改,這個時間也會修改。
3.界面設置
我先貼兩個圖片大家理解一下就好,反正用的控制項不多
主界面一個列表心事所有日記的信息,和一個添加按鈕跳轉寫日記的界面
![這裡寫圖片描述](http://img.blog.csdn.net/20171218141412941?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvejk3OTQ1MTM0MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
寫日記的界面,兩個TextView作為主體,三個按鈕分別承當保存,取消,刪除的功能
,這個刪除的按鈕只會在通過點擊一個日記的信息列表項進入到這個界面才會顯示。
![這裡寫圖片描述](http://img.blog.csdn.net/20171218141446101?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvejk3OTQ1MTM0MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
4.主體邏輯代碼
主界面的邏輯代碼
```
//
// MainViewController.m
// Note
//
// Created by shanreal-iOS on 2017/12/15.
// Copyright © 2017年 shanreal.LongZhenHao. All rights reserved.
//
#import "MainViewController.h"
#import "MainView.h"
#import "MainModel.h"
#import "MainTableViewCell.h"
#import "DetailViewController.h"
#import "NoteBean.h"
@interface MainViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)MainView* mainview;
@property(nonatomic,strong)MainModel* model;
@property(nonatomic,strong)NSMutableArray *dataArray;
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.hidden=YES;
self.navigationController.navigationBar.barStyle=UIBarStyleBlack;
self.mainview = [[MainView alloc]initWithFrame:self.view.frame];
[self.mainview viewInit];
[self.mainview.tableview_main setSeparatorStyle:UITableViewCellSeparatorStyleNone];
self.mainview.tableview_main.bounces=NO;
self.mainview.tableview_main.delegate=self;
self.mainview.tableview_main.dataSource=self;
[self.mainview.btn_add addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.mainview];
/*
RLMResults *delete = [NoteBean allObjects];
RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
for (NoteBean *bean in delete) {
[realm deleteObject:bean];
}
}];
*/
NSString* a =[TimeStampUtil getCurrentTimeStemp];
NSLog(a);
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.dataArray = [NSMutableArray new];
RLMResults *data = [[NoteBean allObjects] sortedResultsUsingKeyPath:@"date" ascending:NO];
[[RLMRealm defaultRealm] transactionWithBlock:^{
for (NoteBean *bean in data) {
[self.dataArray addObject:bean];
}
}];
[self.mainview.tableview_main reloadData];
NSLog(@"%d",self.dataArray.count);
}
-(void)addAction{
DetailViewController* vc = [[DetailViewController alloc]init];
vc.sort = 0;
[self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:@"MainTableViewCell%ld%ld", [indexPath section], [indexPath row]];
MainTableViewCell *cell = (MainTableViewCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[MainTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NoteBean* bean = self.dataArray[indexPath.row];
cell.label_title.text = bean.title;
cell.label_date.text = bean.date;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50*MY;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[theTableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"selected %ld row", indexPath.row);
DetailViewController* vc = [[DetailViewController alloc]init];
vc.sort = 1;
vc.date = ((NoteBean*)self.dataArray[indexPath.row]).date;
vc.title = ((NoteBean*)self.dataArray[indexPath.row]).title;
vc.content = ((NoteBean*)self.dataArray[indexPath.row]).content;
[self.navigationController pushViewController:vc animated:YES];
}
@end
```
寫日期的界面的邏輯代碼
```
#import <UIKit/UIKit.h>
#import "DetailView.h"
#import "DetailModel.h"
#import "NoteBean.h"
@interface DetailViewController : UIViewController
@property(nonatomic,assign)int sort;
@property(nonatomic,strong)NSString* date;
@property(nonatomic,strong)NSString* title;
@property(nonatomic,strong)NSString* content;
@end
//
// DetailViewController.m
// Note
//
// Created by shanreal-iOS on 2017/12/15.
// Copyright © 2017年 shanreal.LongZhenHao. All rights reserved.
//
#import "DetailViewController.h"
@interface DetailViewController ()
@property(nonatomic,strong)DetailView* detailview;
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationController.navigationBar.hidden=YES;
self.navigationController.navigationBar.barStyle=UIBarStyleBlack;
self.detailview = [[DetailView alloc]initWithFrame:self.view.frame];
[self.detailview viewInit];
[self.detailview.btn_save addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
[self.detailview.btn_back addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
[self.detailview.btn_delete addTarget:self action:@selector(deleteAction) forControlEvents:UIControlEventTouchUpInside];
self.detailview.tf_title.text = self.title;
self.detailview.tv_content.text = self.content;
[self.view addSubview:self.detailview];
if(self.sort == 1)
self.detailview.btn_delete.hidden = NO;
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)deleteAction{
NSLog(@"delete");
NSPredicate *pred = [NSPredicate predicateWithFormat:@"date = %@",
self.date];
RLMResults<NoteBean *> *beans = [NoteBean objectsWithPredicate:pred];
RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
NoteBean *bean = [beans objectAtIndex:0];
[realm deleteObject:bean];
}];
[ShowToastView showToastView:self.view WithMessage:@"刪除成功"];
[self performSelector:@selector(cancelAction) withObject:nil afterDelay:2];
}
-(void)saveAction{
NSString* title = self.detailview.tf_title.text;
NSString* content = self.detailview.tv_content.text;
if([title isEqualToString:@""]||title==NULL){
[ShowToastView showToastView:self.view WithMessage:@"標題沒寫"];
return ;
}
if([content isEqualToString:@""]||content==NULL){
[ShowToastView showToastView:self.view WithMessage:@"內容沒寫"];
return ;
}
NSLog(@"save %@ %@",title,content);
if(self.sort == 0){
NoteBean* bean = [[NoteBean alloc]init];
bean.date = [TimeStampUtil getCurrentTimeStemp];
bean.title = self.detailview.tf_title.text;
bean.content = self.detailview.tv_content.text;
RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
[realm addObject:bean];
}];
[ShowToastView showToastView:self.view WithMessage:@"保存成功"];
}else if(self.sort == 1){
NSPredicate *pred = [NSPredicate predicateWithFormat:@"date = %@",
self.date];
RLMResults<NoteBean *> *beans = [NoteBean objectsWithPredicate:pred];
[[RLMRealm defaultRealm] transactionWithBlock:^{
NoteBean *bean = [beans objectAtIndex:0];
bean.date = [TimeStampUtil getCurrentTimeStemp];
bean.title = self.detailview.tf_title.text;
bean.content = self.detailview.tv_content.text;
}];
[ShowToastView showToastView:self.view WithMessage:@"修改成功"];
}
[self performSelector:@selector(cancelAction) withObject:nil afterDelay:2];
}
-(void)cancelAction{
NSLog(@"cancel");
[self.navigationController popViewControllerAnimated:YES];
}
@end
```
最後我奉上源代碼地址
http://download.csdn.net/download/z979451341/10163474