文档章节

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

没有更多内容

加载失败,请刷新页面

加载更多

jenkins+docker+nodejs项目的自动部署环境

1、服务器安装docker yum install docker 2、jenkins安装 //创建jenkins工作目录mkdir -p /var/www/jenkins///拉取docker jenkins镜像docker pull jenkins //设置目前权限(必须...

西园里的猫
14分钟前
1
0
 索引和锁底层原理 !

索引和锁底层原理 ! 一、索引 在之前,我对索引有以下的认知: 索引可以加快数据库的检索速度; 表经常进行INSERT/UPDATE/DELETE操作就不要建立索引了,换言之:索引会降低插入、删除、修改...

DemonsI
24分钟前
0
0
转载:Java实现的SFTP

JSch - Java实现的SFTP(文件上传详解篇):https://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html JSch - Java实现的SFTP(文件下载详解篇):https://www.cnblogs.com/longyg/......

WJtiny
26分钟前
0
0
聊聊storm的AssignmentDistributionService

序 本文主要研究一下storm的AssignmentDistributionService AssignmentDistributionService storm-2.0.0/storm-server/src/main/java/org/apache/storm/nimbus/AssignmentDistributionServic......

go4it
28分钟前
0
0
完全跨域的单点登录(SSO)解决方案源码解析

本文介绍的是一种PHP的开源SSO解决方案,可完全跨域,实现较简洁,源码地址:https://github.com/legalthings/sso 实现原理 一共分为3个角色: Client - 用户的浏览器 Broker - 用户访问的网...

庞陆阳
28分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部