以前理解单独的数据控制器,总是逃不出.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 mainBundle] pathForResource:plistName ofType:@"plist"]; NSMutableArray *plistArray=[[NSMutableArray alloc]initWithContentsOfFile:path]; return plistArray; } -(NSInteger)numbersOfSectionsInPlist:(NSString *)plistName { NSString *path=[[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"]; NSMutableArray *plistArray=[[NSMutableArray alloc]initWithContentsOfFile:path]; return plistArray.count; } -(NSString *)SectionTitle:(NSInteger)section InPlist:(NSString*)plistName { NSString *path=[[NSBundle mainBundle] pathForResource: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 mainBundle] pathForResource: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 mainBundle] pathForResource: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 mainBundle] pathForResource:plistNameofType:@"plist"]; NSMutableArray *plistArray=[[NSMutableArrayalloc]initWithContentsOfFile:path]; return plistArray.count; 调用的时候就直接拿类当对象了: -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [myData numbersOfSectionsInPlist:@"group"]; //返回Section数 } |
数据接口的实现(Data_Interface)
最新推荐文章于 2024-05-11 18:08:06 发布