iOS Note项目

这篇博客详细介绍了如何在iOS应用中使用SQLite数据库进行Note管理。首先讲解了SQLite工具类的使用,接着通过一个实际的Note项目,展示了如何实现分页查询、添加、修改和删除Note的功能。代码示例包括MasterViewController、AddNoteViewController和DetailViewController的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

和上一个Android Note项目一样,先介绍了sqlite的工具类,然后现在我们通过一个Note项目去实现它。

好了,我们开始上图:

同样主要的实现功能有,分页查询,添加Note,修改Note和删除Note。

  好了,该贴代码了,先是实现展示数据:


#import "MasterViewController.h"

@interface MasterViewController ()

@property NSMutableArray *objects;
@end

@implementation MasterViewController

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDatas:) name:@"updateDatas" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    
    //初始化数据操作类
    self.dbAdapter = [[DatabaseAdapter alloc] init];
    
    //设置UInavigationBar颜色
    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    navigationBar.barTintColor = [UIColor colorWithRed:224.0/255 green:0.0/255 blue:81.0/255 alpha:1.0];
    navigationBar.tintColor = [UIColor whiteColor];
    NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    navigationBar.titleTextAttributes = dict;
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
//    [self setupDatas];
    [self initDatas];    
}

//添加测试数据
- (void) setupDatas {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *createDate = [formatter stringFromDate:[NSDate date]];
    NSString *updateDate = createDate;
    NSString *title = [[NSString alloc] initWithFormat:@"Jack"];
    NSString *content = [[NSString alloc] initWithFormat:@"Jack of all trades and master of none."];
    
    Note *note = [[Note alloc] initWithTitle:title content:content createDate:createDate updateDate:updateDate];
    [self.dbAdapter create:note];
}

//初始化数据
- (void) initDatas {
   self.objects = [self.dbAdapter findLimit:20 withSkip:0];
}

-(void)updateDatas:(NSNotification*) notification {
    [self.objects removeAllObjects];
    [self.objects addObjectsFromArray:[self.dbAdapter findLimit:20 withSkip:0]];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Segues

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        Note *note = self.objects[indexPath.row];
        [[segue destinationViewController] setNote:note];
    }
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.objects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    Note *note = self.objects[indexPath.row];
    
    //设置为圆形
    cell.lblLetter.layer.cornerRadius = 30.0;
    cell.lblLetter.layer.masksToBounds = YES;
    
    //设置随机背景色
    CGFloat red = (arc4random() % 256) / 255.0;
    CGFloat green = (arc4random() % 256) / 255.0;
    CGFloat blue = (arc4random() % 256) / 255.0;
    [cell.lblLetter setBackgroundColor:[UIColor colorWithRed:red green:green blue:blue alpha:1.0]];
    
    cell.lblLetter.text = [note.title substringToIndex:1];
    cell.lblTitle.text = note.title;
    cell.lblSubTitle.text = note.content;
    cell.lblDate.text = note.updateDate;
    
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        Note *note = self.objects[indexPath.row];
        [self.dbAdapter remove:note._id];
        [self.objects removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}


@end

MasterViewController.h代码如下:


#import <UIKit/UIKit.h>
#import "DatabaseAdapter.h"
#import "Note.h"
#import "TableViewCell.h"
#import "DetailViewController.h"

@interface MasterViewController : UITableViewController

@property (nonatomic, strong) DatabaseAdapter *dbAdapter;

- (void) setupDatas;

- (void) initDatas;

@end

我根据平台的不同,在iOS上直接在主界面就添加了删除功能,这也算在不同平台有不同特色的设计特点吧。


接下来实现的是添加Note功能,代码如下:

#import "AddNoteViewController.h"

@interface AddNoteViewController ()

@end

@implementation AddNoteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(addNote:)];
    self.navigationItem.rightBarButtonItem = addButton;
    
    self.dbAdapter = [[DatabaseAdapter alloc] init];
}

- (void)addNote:(id)sender {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *createDate = [formatter stringFromDate:[NSDate date]];
    NSString *updateDate = createDate;
    NSString *title = self.txtTitle.text;
    NSString *content = self.txtContent.text;
    Note *note = [[Note alloc] initWithTitle:title content:content createDate:createDate updateDate:updateDate];
    
    [self.dbAdapter create:note];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateDatas" object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

AddNoteViewController.h代码如下:


#import <UIKit/UIKit.h>
#import "DatabaseAdapter.h"
#import "Note.h"

@interface AddNoteViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *txtTitle;

@property (weak, nonatomic) IBOutlet UITextView *txtContent;

@property (nonatomic, strong) DatabaseAdapter *dbAdapter;

@end


最后我们实现的是修改Note,代码如下:


#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveNote:)];
    self.navigationItem.rightBarButtonItem = saveButton;
    
    self.dbAdapter = [[DatabaseAdapter alloc] init];
    
    [self initDatas];
}

- (void) initDatas {
    self.txtTitle.text = self.note.title;
    self.txtContent.text = self.note.content;
}

- (void)saveNote:(id)sender {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *updateDate = [formatter stringFromDate:[NSDate date]];
    NSString *title = self.txtTitle.text;
    NSString *content = self.txtContent.text;
    Note *note = [[Note alloc] initWithID:self.note._id title:title content:content updateDate:updateDate];
    
    [self.dbAdapter update:note];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateDatas" object:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

DetailViewController.h代码如下:

#import <UIKit/UIKit.h>
#import "Note.h"
#include "DatabaseAdapter.h"
#import "MasterViewController.h"

@interface DetailViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *txtTitle;

@property (weak, nonatomic) IBOutlet UITextView *txtContent;

@property (nonatomic, strong) DatabaseAdapter *dbAdapter;

@property (nonatomic, strong) Note *note;

- (void) initDatas;

@end


项目代码链接: http://pan.baidu.com/s/1o6w6f8m 密码: t56u


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值