文档章节

coredata 单表

C
 CBETA-陈
发布于 2016/07/29 11:40
字数 844
阅读 12
收藏 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是苹果为我们封装的一个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
基于Xcode8.x的CoreData的使用

首先说说对于CoreData的理解,类似于FMDB。他能将数据库中的列表转换成模型实体对象,方便开发者使用。同时,也能将一个将实体对象转换成数据库中的表,存储到数据库中。这就是CoreData,数据...

最牛逼的代码搬运工
2016/12/07
132
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

spring 事件

ContextRefreshedEvent Event raised when an {@code ApplicationContext} gets initialized or refreshed. ContextClosedEvent Event raised when an {@code ApplicationContext} gets clos......

Canaan_
34分钟前
1
0
leetcode两数之和

leetcode中求两数之和解决方法 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。 给定 nums = [2, 7, 11, ...

lar555
50分钟前
1
0
js实现限制网页内容复制

转载 在我们做的网页发到网上后,如果访客看到比较喜欢的内容,只要复制就可以变为自己的,自己辛辛苦苦弄半天还不及人家的一下复制,有时为了只让访客看到,而不能让它们复制内容,就用Jav...

lc_comeon
54分钟前
1
0
jenkins将spring boot项目发布到阿里云镜像中

1、spring boot项目 1.1 pom.xml配置 <artifactId>xxx-docker</artifactId><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.......

xixingzhe
今天
0
0
qsv格式可以在电视上播放吗

  大家都知道qsv格式是爱奇艺的独家缓存格式,是加密的,一般的播放器是无法播放的,只能在爱奇艺播放器上播放,如果想要在电视上播放,就必须要安装爱奇艺播放器,比较麻烦。其实还有一种...

萤火的萤火
今天
1
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部