文档章节

coredata 单表

C
 CBETA-陈
发布于 2016/07/29 11:40
字数 844
阅读 13
收藏 0

//业务处理层

#import <Foundation/Foundation.h>

@interface DataBase : NSObject

+(instancetype)sharedDataBase;

-(void)insertData:(NSString *)name sing:(NSString *)sing;

-(NSArray *)showData;

 

@end

#import "DataBase.h"
#import "AppDelegate.h"
#import "DataEntity.h"

@interface DataBase ()

@property(nonatomic,strong)AppDelegate *myDelegate;
@property(nonatomic,strong)NSMutableArray *allArr;

@end

static DataBase *sharedDataBase =nil;
@implementation DataBase

+(instancetype)sharedDataBase
{
    if (sharedDataBase ==nil) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            sharedDataBase =[[DataBase alloc]init];
        });
    }
    return sharedDataBase;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone
{
    if (sharedDataBase ==nil) {
        sharedDataBase =[super allocWithZone:zone];
    }
    return sharedDataBase;
}
-(void)insertData:(NSString *)name sing:(NSString *)sing
{
    self.allArr = [NSMutableArray array];
    self.myDelegate = [UIApplication sharedApplication].delegate;
    
    DataEntity *entity = [[DataEntity alloc]initWithEntity:[NSEntityDescription entityForName:@"DataEntity" inManagedObjectContext:self.myDelegate.managedObjectContext] insertIntoManagedObjectContext:self.myDelegate.managedObjectContext];
    entity.name = name;
    entity.sing = sing;
    
}

-(NSArray *)showData
{
    NSArray *arr = [self.myDelegate.managedObjectContext executeFetchRequest:[NSFetchRequest fetchRequestWithEntityName:@"DataEntity"] error:nil];
    NSLog(@"arr:%@",arr);
    return arr;
    
}

#import "OneViewController.h"
#import "MusicPlayViewController.h"
#import "ThreeViewController.h"

@interface OneViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *table;
    NSArray *arr;
}

@end

//第一界面

@implementation OneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationItem.title =@"月考";
    self.navigationController.navigationBar.backgroundColor =[UIColor redColor];
    
    UIBarButtonItem *rightItem =[[UIBarButtonItem alloc]initWithTitle:@"显示收藏" style:UIBarButtonItemStyleDone target:self action:@selector(push)];
    self.navigationItem.rightBarButtonItem=rightItem;
    
    table =[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    table.dataSource=self;
    table.delegate=self;
    [self.view addSubview:table];
    
    [self getArr];
}
-(void)push{
    ThreeViewController *tVC =[[ThreeViewController alloc]init];
    [self.navigationController pushViewController:tVC animated:YES];
}
-(void)getArr
{
    NSString *path =[[NSBundle mainBundle]pathForResource:@"s.plist" ofType:nil];
    arr =[NSArray arrayWithContentsOfFile:path];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section ==0) {
        return 2;
    }else if (section ==1){
        return 3;
    }else if (section ==2){
        return 1;
    }else{
        return 2;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str =@"s";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:str];
    if (cell==nil) {
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
    }
    if (indexPath.section ==0) {
        NSDictionary *dict =[arr objectAtIndex:1];
        cell.textLabel.text =[dict objectForKey:@"name"];
        cell.detailTextLabel.text =[dict objectForKey:@"sing"];
    }else if (indexPath.section ==1){
        if (indexPath.row ==0) {
            NSDictionary *dict =[arr objectAtIndex:2];
            cell.textLabel.text =[dict objectForKey:@"name"];
            cell.detailTextLabel.text =[dict objectForKey:@"sing"];
        }
        if (indexPath.row ==1) {
            NSDictionary *dict =[arr objectAtIndex:3];
            cell.textLabel.text =[dict objectForKey:@"name"];
            cell.detailTextLabel.text =[dict objectForKey:@"sing"];
        }
        if (indexPath.row ==2) {
            NSDictionary *dict =[arr objectAtIndex:4];
            cell.textLabel.text =[dict objectForKey:@"name"];
            cell.detailTextLabel.text =[dict objectForKey:@"sing"];
        }
    }
    else if (indexPath.section ==2)
    {
        NSDictionary *dict =[arr objectAtIndex:5];
        cell.textLabel.text =[dict objectForKey:@"name"];
        cell.detailTextLabel.text =[dict objectForKey:@"sing"];

    }else{
        if (indexPath.row ==0) {
            NSDictionary *dict =[arr objectAtIndex:6];
            cell.textLabel.text =[dict objectForKey:@"name"];
            cell.detailTextLabel.text =[dict objectForKey:@"sing"];
        }
        if (indexPath.row ==1) {
            NSDictionary *dict =[arr objectAtIndex:7];
            cell.textLabel.text =[dict objectForKey:@"name"];
            cell.detailTextLabel.text =[dict objectForKey:@"sing"];
        }
    }
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MusicPlayViewController *mVC =[[MusicPlayViewController alloc]init];
    
    if (indexPath.section ==0) {
        NSDictionary *dic =[arr objectAtIndex:1];
        mVC.name =dic[@"name"];
        mVC.sing =dic[@"sing"];
    }else if (indexPath.section ==1){
        if (indexPath.row ==0) {
            NSDictionary *dic =[arr objectAtIndex:2];
            mVC.name =dic[@"name"];
            mVC.sing =dic[@"sing"];
        }
    }
    if (indexPath.row ==1) {
        NSDictionary *dic =[arr objectAtIndex:3];
        mVC.name =dic[@"name"];
        mVC.sing =dic[@"sing"];
    }
    if (indexPath.row ==2) {
        NSDictionary *dic =[arr objectAtIndex:4];
        mVC.name =dic[@"name"];
        mVC.sing =dic[@"sing"];
    }
    else if (indexPath.section ==2){
        NSDictionary *dic =[arr objectAtIndex:5];
        mVC.name =dic[@"name"];
        mVC.sing =dic[@"sing"];
    }
    if (indexPath.row ==0) {
        NSDictionary *dic =[arr objectAtIndex:6];
        mVC.name =dic[@"name"];
        mVC.sing =dic[@"sing"];
    }
    if (indexPath.row ==1) {
        NSDictionary *dic =[arr objectAtIndex:7];
        mVC.name =dic[@"name"];
        mVC.sing =dic[@"sing"];
    }
    
    [self.navigationController pushViewController:mVC animated:YES];
    
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section ==0) {
        return [arr objectAtIndex:1][@"singer"];
    }else if (section ==1){
        return [arr objectAtIndex:3][@"singer"];
    }else if (section ==2){
        return [arr objectAtIndex:5][@"singer"];
    }else{
        return [arr objectAtIndex:7][@"singer"];
    }
}


#import <UIKit/UIKit.h>

@interface MusicPlayViewController : UIViewController

@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *sing;


@end

//
第二界面

#import "MusicPlayViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "DataBase.h"

@interface MusicPlayViewController ()
@property(nonatomic,strong)AVAudioPlayer *player;

@end

@implementation MusicPlayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    
    UIBarButtonItem *rightItem =[[UIBarButtonItem alloc]initWithTitle:@"加入收藏" style:UIBarButtonItemStyleDone target:self action:@selector(save)];
    self.navigationItem.rightBarButtonItem=rightItem;
    
    UIButton *b=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    b.frame=CGRectMake(100, 400, 100, 40);
    [b setTitle:@"~play~" forState:UIControlStateNormal];
    [b addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:b];

}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    NSString *path =[[NSBundle mainBundle] pathForResource:@"1" ofType:@"mp3"];
    NSURL *url =[NSURL fileURLWithPath:path];
    _player =[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
}

-(void)click
{
    [_player play];
}
-(void)save
{
    [[DataBase sharedDataBase]insertData:self.name sing:self.sing];
}


@end

//
第三界面

#import "ThreeViewController.h"
#import "DataBase.h"
#import "DataEntity.h"

@interface ThreeViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *table;
    NSArray *arr;
}
@end

@implementation ThreeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    
    table =[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    table.dataSource=self;
    table.delegate=self;
    [self.view addSubview:table];

}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    arr =[[DataBase sharedDataBase]showData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return arr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str =@"s";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:str];
    if (cell==nil) {
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
    }
    DataEntity *entity =arr[indexPath.row];
    cell.textLabel.text =entity.name;
    cell.detailTextLabel.text =entity.sing;
    return cell;
}

 

@end

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>name</key>
    <string>天高地厚</string>
    <key>sing</key>
    <string>《天高地厚》</string>
    <key>singer</key>
    <string>阿信</string>
</dict>
</plist>

 

© 著作权归作者所有

共有 人打赏支持
C
粉丝 0
博文 5
码字总数 3395
作品 0
朝阳
私信 提问
CoreData基本使用(更新:储存数组、字典类型)

前几天项目里面加入了CoreData,第一次用,记录一下,这里只记录创建项目之后添加的CoreData。 demo地址: CoreDataDemo 1. 创建CoreData模型文件 2. 创建项目中需要用到的实体(Entities),...

YvanLiu
05/02
0
0
为CoreData封装一个简易的操作类

前言 在创建一个项目时,如果选择了CoreData,那么会在AppDelegate类中为我们创建一些和CoreData相关的代码,我们把这些代码提取出来,稍作修改便可以得到一个适应我们自己项目的CoreData操作...

DragonFK
2015/08/28
10
0
CoreData 专业术语整理

前言 CoreData是苹果为我们封装的一个ORM(对象关系映射)库,可以很方便的把数据库中的数据转为一个对象,把一个对象保存到数据库中。如同微软的Entity Framework、Java的hibernate~ 在学习...

DragonFK
2015/08/28
0
0
CoreData数据管理造成Crash:Can't find model for source store

场景再现:上线后出现大量crash,crash发生在iOS8及以下设备中。 bug.png devices.png 问题解决:真机直接调试没问题,没有crash。后来只能在release下进行调试。首先去处release的debug限制...

智小融
2017/12/04
0
0
iOS中CoreData数据管理系列一——初识CoreData

iOS中CoreData数据管理系列一——初识CoreData 一、何为CoreData CoreData是一个专门用来管理数据的框架,其在性能与书写方便上都有很大的优势,在数据库管理方面,apple强烈推荐开发者使用C...

珲少
2016/01/27
1K
0

没有更多内容

加载失败,请刷新页面

加载更多

vuex进阶知识点巩固

我们先回忆一下上一篇的代码 computed:{ getName(){ return this.$store.state.name }} 这里假设现在逻辑有变,我们最终期望得到的数据(getName),是基于 this.$store.state.na...

嫣然丫丫丫
12分钟前
1
0
Python出现安全策略问题的解决方法

Python运行期间出现如下错误 import: attempt to perform an operation not allowed by the security policy `PS' @ error/constitute.c/IsCoderAuthorized/408. 解决方法:在脚本的开头添加......

大糊涂
19分钟前
1
0
Angularjs实现控制器之间通信方式示例

利用angularjs开发项目中,控制器之间的通信,比如参数的传递,数据的传递,都是比较常见的。控制器之间的通信,显得尤为重要。常见的方式有如下两种:一、angular服务的方式;二、基于事件广...

前端攻城老湿
27分钟前
1
0
xshell使用xftp传输文件

12月11日任务 15.4 xshell使用xftp传输文件 15.5 使用pure-ftpd搭建ftp服务 1.xshell使用xftp传输文件 示例一:xshell使用sftp传输文件 新建一个会话 定义为sftp 连接登入 可以get文件,下载...

hhpuppy
29分钟前
2
0
深入解析Vuex实战总结

这篇文章主要介绍了Vuex的初探与实战小结,写的十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参考学习下。如有不足之处,欢迎批评指正。 1.背景 最近在做一个单页面的管理后台项...

前端攻城小牛
30分钟前
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部