接上,创建MainListOneLabelCollectionViewCell
MainListOneLabelCollectionViewCell.h中
//创建属性
@property (nonatomic, strong) UILabel *titleLabel;
MainListOneLabelCollectionViewCell.m中
//初始化,并布局
- (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);
}
//创建MainListTimeLabelCollectionViewCell
MainListTimeLabelCollectionViewCell.m中
//添加控件
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *timeLabel;
- (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;
}
#warning 这里添加了新代码
- (void)bindModel:(FileMessage *)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, self.frame.size.height / 2);
self.timeLabel.frame = CGRectMake(0, self.frame.size.height / 2, self.frame.size.width, self.frame.size.height / 2);
}
//创建MainListCollectionReusableView
MainListCollectionReusableView.h中
//创建控件
@property (nonatomic, strong) UILabel *titleLabel;
MainListCollectionReusableView.m中
- (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);
}
创建NoteViewController
NoteViewController.h中
//声明属性
@property (nonatomic, assign) NSInteger FID;
NoteViewController.m中
引入头文件
#import "NoteListTableViewCell.h"
#import "AddNoteViewController.h"
#import "DataBaseHandle.h"
#import "WordNote.h"
声明协议<UITableViewDataSource, UITableViewDelegate>
添加控件
@property (nonatomic, strong) UITableView *tableView;
调用父类方法,并对TableView进行布局
- (void)loadData
{
[super loadData];
DataBaseHandle *dataBaseHandle = [DataBaseHandle sharedDataBaseHandle];
[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;
}
- (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];
[self.navigationController pushViewController:addNoteVC animated:YES];
}
- (void)barButtonItemClicked
{
AddNoteViewController *addNoteVC = [[AddNoteViewController alloc] init];
addNoteVC.FID = self.FID;
#warning 这里添加新代码了
// 使用block回调 调用loadData方法
typeof(self) pSelf = self;
addNoteVC.myBlock = ^() {
[pSelf loadData];
};
[self.navigationController pushViewController:addNoteVC animated:YES];
}
#warning 这里添加了新代码
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// 开始删除
DataBaseHandle *dataBaseHandle = [DataBaseHandle sharedDataBaseHandle];
[dataBaseHandle openDB];
// 获取要删除的那个model
WordNote *wordNote = self.dataArray[indexPath.row];
// 根据WID删除数据
[dataBaseHandle deleteFromWordNoteWithWID:wordNote.WID];
// 更新数据源
[self.dataArray removeObjectAtIndex:indexPath.row];
// 刷新UI
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}