coredata 单表
coredata 单表
CBETA-陈 发表于1年前
coredata 单表
  • 发表于 1年前
  • 阅读 12
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: /////

//业务处理层

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

 

共有 人打赏支持
粉丝 0
博文 5
码字总数 3395
×
CBETA-陈
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: