ios——记事本demo

 

实现的功能包括 关联数据库,可以增加记事本 、 查看记事本、 修改记事本内容 , 每个记事本的详情页面可以显示保存记事本的当前时间 。使用到UICollectionView组件。 

 

界面如下。

  

因为用到了数据库,所以需要导入fmdb文件,这里采取的是手动导入的方式。

UIcollectionview的使用有几个注意点。首先需要引用UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout协议。 然后是layout,创建UIcollectionview需要使用到这个layout ,然后和UItableview类似的 delegate 、dataSource 也是不能少的

UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    self.collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 40, self.view.bounds.size.width, self.view.bounds.size.height-30)  collectionViewLayout:layout];  //刚开始没有UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init]; 和collectionViewLayout:layout  报错“UICollectionView must be initialized with a non-nil layout parameter”

然后需要通过registerClass 注册cell

[self.collectionView registerClass:[NoteCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];   //注册cell
    //没有这一句也会报错 “could not dequeue a view of kind: UICollectionElementKindCell with identifie”

在cellForItemAtIndexPath中 dequeueReusableCell 重用

 NoteCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];

下面就是正式的开发了。

viewcontroller文件的viewdidLoad方法定义如下。

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor darkGrayColor];
    self.edgesForExtendedLayout=UIRectEdgeNone;
    self.title=@"记事本";
    
    UIButton *addBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [addBtn addTarget:self action:@selector(addnew) forControlEvents:UIControlEventTouchUpInside];
    [addBtn setTitle:@"+" forState:UIControlStateNormal];
    addBtn.titleLabel.font=[UIFont systemFontOfSize:24];
    [addBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    UIBarButtonItem *rightBtn=[[UIBarButtonItem alloc]initWithCustomView:addBtn];
    self.navigationItem.rightBarButtonItem=rightBtn;
    
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    self.collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 40, self.view.bounds.size.width, self.view.bounds.size.height-30)  collectionViewLayout:layout];  //刚开始没有UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init]; 和collectionViewLayout:layout  报错“UICollectionView must be initialized with a non-nil layout parameter”
    self.collectionView.delegate=self;
    self.collectionView.dataSource=self;
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[NoteCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];   //注册cell
    //没有这一句也会报错 “could not dequeue a view of kind: UICollectionElementKindCell with identifie”
    layout.minimumLineSpacing=10;
    
    self.ediBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [self.ediBtn addTarget:self action:@selector(edit:) forControlEvents:UIControlEventTouchUpInside];
    [self.ediBtn setTitle:@"编辑" forState:UIControlStateNormal];
    [self.ediBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    UIBarButtonItem *leftBtn=[[UIBarButtonItem alloc]initWithCustomView:self.ediBtn];
    self.navigationItem.leftBarButtonItem=leftBtn;
    
    //搜索栏
    self.searchController=[[UISearchController alloc]initWithSearchResultsController:nil];
    //设置代理
    self.searchController.delegate=self;
    self.searchController.searchResultsUpdater=self;
    self.searchController.searchBar.placeholder=@"请输入要搜索的内容";
    self.searchController.searchBar.frame=CGRectMake(0, 0, self.view.bounds.size.width, 40);
    self.definesPresentationContext=YES;   //搜索栏的偏移问题还没解决???
    self.searchController.dimsBackgroundDuringPresentation=NO;   //搜索时,背景变暗
    self.searchController.obscuresBackgroundDuringPresentation=NO;   //搜索时,背景变模糊
    [self.view addSubview:self.searchController.searchBar];
    
    self.dataArray=[[NSMutableArray alloc]init];  //创建数组
    self.searchArray=[[NSMutableArray alloc]init]; 
    manager = [FMDBManager s
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值