数据接口的实现(Data_Interface)

以前理解单独的数据控制器,总是逃不出.h内部必须包含对象的怪圈,得到牛人指点,尝试写了一下,才开始有所领悟。

自己的Interface可以理解为针对某一种类型的(尤其是通用类型)写自己的fuction,得到相应的结果,设计部分要注意参数设置,以及对各种可能做出重点调试,熟练一些的加点内存管理神马的,目前我只是能实现最基本的方法。

以下是最近写的,容易理解的对象方法:

#import <Foundation/Foundation.h>


@interface myData : NSObject

-(NSMutableArray *)getPlistDataFromPlist: (NSString *)plistName;

-(NSInteger)numbersOfSectionsInPlist:(NSString *)plistName;

-(NSString *)SectionTitle:(NSInteger)section InPlist:(NSString*)plistName;


-(NSInteger)RowsNumberInSection:(NSInteger)section InPlist:(NSString*)plistName;

-(NSString *)cellTitleInSection:(NSInteger)section atRow:(NSInteger)row InPlist:(NSString*)plistName;

@end


.m文件:

#import "myData.h"


@implementation myData


-(NSMutableArray *)getPlistDataFromPlist: (NSString *)plistName

{

    NSString *path=[[NSBundle mainBundlepathForResource:plistName ofType:@"plist"];

    NSMutableArray *plistArray=[[NSMutableArray alloc]initWithContentsOfFile:path];

    return plistArray;

}

-(NSInteger)numbersOfSectionsInPlist:(NSString *)plistName

{

    NSString *path=[[NSBundle mainBundlepathForResource:plistName ofType:@"plist"];

    NSMutableArray *plistArray=[[NSMutableArray alloc]initWithContentsOfFile:path];

    return plistArray.count;


}


-(NSString *)SectionTitle:(NSInteger)section InPlist:(NSString*)plistName

{

    NSString *path=[[NSBundle mainBundlepathForResource:plistName ofType:@"plist"];

    NSMutableArray *plistArray=[[NSMutableArray alloc]initWithContentsOfFile:path];

    NSDictionary *sectionDic=[plistArray objectAtIndex:section];

    NSString *sectionTitle=[sectionDic objectForKey:@"Grade"];

    return sectionTitle;

}


-(NSInteger)RowsNumberInSection:(NSInteger)section InPlist:(NSString*)plistName

{

    NSString *path=[[NSBundle mainBundlepathForResource:plistName ofType:@"plist"];

    NSMutableArray *plistArray=[[NSMutableArray alloc]initWithContentsOfFile:path];

    NSDictionary *dic=[plistArray objectAtIndex:section];

    NSArray *array=[dic objectForKey:@"Class"];

    return array.count;

}


-(NSString *)cellTitleInSection:(NSInteger)section atRow:(NSInteger)row InPlist:(NSString*)plistName

{

    NSString *path=[[NSBundle mainBundlepathForResource:plistName ofType:@"plist"];

    NSMutableArray *plistArray=[[NSMutableArray alloc]initWithContentsOfFile:path];

    NSDictionary *cellDic=[plistArray objectAtIndex:section];

    NSArray *cellArray=[cellDic objectForKey:@"Class"];

    NSDictionary *classDic=[cellArray objectAtIndex:row];

    

    NSString *cellTextTitle=[classDic objectForKey:@"classname"];

    return cellTextTitle;

}

@end


调用例子:

myData *myDataX=[[myData alloc]init];

    return [myDataX numbersOfSectionsInPlist:@"group"];


当然,这样貌似有些麻烦,每次调用还都要新建对象,所以更好的办法是将对象方法统一改成class方法。

以往的tableViewcell数据要写一长串,现在只要:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    

    NSInteger section=[indexPath section];  //获取section

    NSInteger row = [indexPath row];    //获取行号

    

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierforIndexPath:indexPath];

    

    myData *myDataX=[[myData alloc]init];

    cell.textLabel.text=[myDataX cellTitleInSection:section atRow:row InPlist:@"group"];

    return cell;

    

}


当然,适合自己的才是最好的,要仔细分析要解析的数据结构。

当用类方法实现的时候:myData.h:

@interface myData : NSObject

+(NSMutableArray *)getPlistDataFromPlist: (NSString *)plistName;

+(NSInteger)numbersOfSectionsInPlist:(NSString *)plistName;

+(NSString *)SectionTitle:(NSInteger)section InPlist:(NSString*)plistName;


+(NSInteger)RowsNumberInSection:(NSInteger)section InPlist:(NSString*)plistName;

+( NSString  *)cellTitleInSection:( NSInteger )section atRow:( NSInteger )row InPlist:( NSString *)plistName;

.m文件,对象方法都改成类方法就ok,减号都变成加号:

+(NSInteger)numbersOfSectionsInPlist:(NSString *)plistName

{

    NSString *path=[[NSBundle mainBundlepathForResource:plistNameofType:@"plist"];

    NSMutableArray *plistArray=[[NSMutableArrayalloc]initWithContentsOfFile:path];

    return plistArray.count;


}

调用的时候就直接拿类当对象了:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return [myData numbersOfSectionsInPlist:@"group"];

    //返回Section

}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

limaning

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值