coredata 单表

//业务处理层

#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>

 

转载于:https://my.oschina.net/chenchen9654321/blog/719993

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值