UI进阶之记事本1
AppDelegate.m中
//引入头文件
#import "MainListViewController.h"
#import "DataBaseHandle.h"
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
MainListViewController *mainListVC = [[MainListViewController alloc] init];
UINavigationController *mainListNaviVC = [[UINavigationController alloc] initWithRootViewController:mainListVC];
self.window.rootViewController = mainListNaviVC;
//DataBaseHandle 实现方法
DataBaseHandle *dataBase = [DataBaseHandle sharedDataBaseHandle];
[dataBase openDB];
[dataBase createWordNoteTable];
[dataBase createfileMessageTable];
[dataBase closeDB];
//创建BaseViewController作为父视图控制器
BaseViewController.h中
//数据源数组
@property(nonatomic, strong) NSMutableArray *dataArray;
//创建视图的方法
- (void)createView;
//获取数据的方法
- (void)loadData;
BaseViewController.m中
- (void)viewDidLoad {
[super viewDidLoad];
[self createView];
self.dataArray = [NSMutableArray array];
[self loadData];
}
- (void)createView
{
}
//获取数据的方法
- (void)loadData
{
//这里会给dataArray赋值
}
//创建MainListViewController
MainListViewController.m中
引入头文件
#import "MainListOneLabelCollectionViewCell.h"
#import "MainListTimeLabelCollectionViewCell.h"
#import "MainListCollectionReusableView.h"
#import "NoteViewController.h"
#import "AddFileViewController.h"
#import "DataBaseHandle.h"
@property (nonatomic, strong) UICollectionView *collectionView;
//调用父类方法并进行布局
- (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;
#warning 这里更新代码了
default:{
MainListTimeLabelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"timeLabelCELL" forIndexPath:indexPath];
#warning 这里添加了新代码
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;
#warning 这里更新代码了
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;
}
}
#warning 这里添加了新代码
// 长按收拾的触发方法
- (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];
}