UITableView_4cell简单练习,汽车品牌展示
2015-06-04 20:03
1036 查看
一、plist文件和项目结构图
说明:这是一个嵌套模型的示例
二、代码示例:
CarGroups.h文件代码:
#import<Foundation/Foundation.h>
@interfaceCarGroups:NSObject
@property(nonatomic,copy)NSString*title;@property(nonatomic,strong)NSArray*cars;-(instancetype)initWithDict:(NSDictionary*)dict;+(instancetype)groupsWithDict:(NSDictionary*)dict;
@end
CarGroups.m文件:
//// CarGroups.m// showCars//// CreatedbyTogeon6/4/15.// Copyright(c)2015wxhl.Allrightsreserved.//
#import"CarGroups.h"#import"CarDes.h"
@implementationCarGroups
-(instancetype)initWithDict:(NSDictionary*)dict{ if(self=[superinit]){ _title=dict[@"title"];// cars也是字典类型的也要转换为模型 NSArray*carArray=dict[@"cars"]; NSMutableArray*temArray=[NSMutableArrayarray]; for(NSDictionary*carDictincarArray){ CarDes*cars=[CarDescarWithDict:carDict]; [temArrayaddObject:cars]; } _cars=temArray; } returnself;}+(instancetype)groupsWithDict:(NSDictionary*)dict{ return[[selfalloc]initWithDict:dict];}
@end
YYcars.h文件
CarDes.h文件
//// CarDes.h// showCars//// CreatedbyTogeon6/4/15.// Copyright(c)2015wxhl.Allrightsreserved.//
#import<Foundation/Foundation.h>
@interfaceCarDes:NSObject
@property(nonatomic,copy)NSString*icon;@property(nonatomic,copy)NSString*name;
-(instancetype)initWithDict:(NSDictionary*)dict;+(instancetype)carWithDict:(NSDictionary*)dict;@end
ViewController.m
//// ViewController.m// showCars//// CreatedbyTogeon6/4/15.// Copyright(c)2015wxhl.Allrightsreserved.//
#import"ViewController.h"#import"CarGroups.h"#import"CarDes.h"
@interfaceViewController()<UITableViewDataSource>//遵守datasource协议
@end
@implementationViewController/** *懒加载 */-(NSArray*)groups{ if(_groups==nil){// 获取文件安装路径 NSString*path=[[NSBundlemainBundle]pathForResource:@"cars_total.plist"ofType:nil]; NSArray*groupArray =[NSArrayarrayWithContentsOfFile:path];// 数组中的字典转成模型 NSMutableArray*tempArray=[NSMutableArrayarray]; for(NSDictionary*dictingroupArray){ CarGroups*car=[CarGroupsgroupsWithDict:dict]; [tempArrayaddObject:car]; }// groups里面的存储的是模型数组 _groups=tempArray; } return_groups;}
-(void)viewDidLoad{ [superviewDidLoad]; //Doanyadditionalsetupafterloadingtheview,typicallyfromanib.// 创建表视图对象 UITableView*tabView=[[UITableViewalloc]initWithFrame:CGRectMake(0,20,375,657)style:UITableViewStylePlain]; // 数据源代理设置 tabView.dataSource=self; [self.viewaddSubview:tabView]; }//返回有多少组数据-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{ returnself.groups.count;}
//返回每组数据有多少条-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{// 拿到对应的组 CarGroups*group=self.groups[section]; // 返回每组有多少条数据 returngroup.cars.count;}-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{// 定义创建cel的标签 staticNSString*ID=@"car";// 在缓存池中查找对应标签的cel对象 UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:ID]; // 判断cell是否为空,是空的要创建 if(cell==nil){ cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ID]; } // 赋值给对应的row// 首先得到对应的车组 CarGroups*group=self.groups[indexPath.section]; CarDes*cars=group.cars[indexPath.row]; cell.textLabel.text=cars.name; cell.imageView.image=[UIImageimageNamed:cars.icon]; returncell; }
//返回组的名称-(NSString*)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)section{ CarGroups*group=self.groups[section]; returngroup.title;}/** *返回右边索引拦现实的字符串数组 */-(NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView{// kvc找不到键为title的值会自动到groups里面的元素找,找到对应的值,封装成数组返回,用valueForKey做不到 NSArray *array= [self.groupsvalueForKeyPath:@"title"]; return array;}@end
实现效果:
说明:这是一个嵌套模型的示例
二、代码示例:
CarGroups.h文件代码:
#import<Foundation/Foundation.h>
@interfaceCarGroups:NSObject
@property(nonatomic,copy)NSString*title;@property(nonatomic,strong)NSArray*cars;-(instancetype)initWithDict:(NSDictionary*)dict;+(instancetype)groupsWithDict:(NSDictionary*)dict;
@end
CarGroups.m文件:
//// CarGroups.m// showCars//// CreatedbyTogeon6/4/15.// Copyright(c)2015wxhl.Allrightsreserved.//
#import"CarGroups.h"#import"CarDes.h"
@implementationCarGroups
-(instancetype)initWithDict:(NSDictionary*)dict{ if(self=[superinit]){ _title=dict[@"title"];// cars也是字典类型的也要转换为模型 NSArray*carArray=dict[@"cars"]; NSMutableArray*temArray=[NSMutableArrayarray]; for(NSDictionary*carDictincarArray){ CarDes*cars=[CarDescarWithDict:carDict]; [temArrayaddObject:cars]; } _cars=temArray; } returnself;}+(instancetype)groupsWithDict:(NSDictionary*)dict{ return[[selfalloc]initWithDict:dict];}
@end
YYcars.h文件
1//
2//YYcars.h
3//07-汽车展示(高级)
4//
5//Createdbyappleon14-5-28.
6//Copyright(c)2014年itcase.Allrightsreserved.
7//
8
9#import<Foundation/Foundation.h>
10
11@interfaceYYcars:NSObject
12@property(nonatomic,copy)NSString*name;
13@property(nonatomic,copy)NSString*icon;
14
15-(instancetype)initWithDict:(NSDictionary*)dict;
16+(instancetype)carsWithDict:(NSDictionary*)dict;
17@end
CarDes.h文件
//// CarDes.h// showCars//// CreatedbyTogeon6/4/15.// Copyright(c)2015wxhl.Allrightsreserved.//
#import<Foundation/Foundation.h>
@interfaceCarDes:NSObject
@property(nonatomic,copy)NSString*icon;@property(nonatomic,copy)NSString*name;
-(instancetype)initWithDict:(NSDictionary*)dict;+(instancetype)carWithDict:(NSDictionary*)dict;@end
ViewController.m
//// ViewController.m// showCars//// CreatedbyTogeon6/4/15.// Copyright(c)2015wxhl.Allrightsreserved.//
#import"ViewController.h"#import"CarGroups.h"#import"CarDes.h"
@interfaceViewController()<UITableViewDataSource>//遵守datasource协议
@end
@implementationViewController/** *懒加载 */-(NSArray*)groups{ if(_groups==nil){// 获取文件安装路径 NSString*path=[[NSBundlemainBundle]pathForResource:@"cars_total.plist"ofType:nil]; NSArray*groupArray =[NSArrayarrayWithContentsOfFile:path];// 数组中的字典转成模型 NSMutableArray*tempArray=[NSMutableArrayarray]; for(NSDictionary*dictingroupArray){ CarGroups*car=[CarGroupsgroupsWithDict:dict]; [tempArrayaddObject:car]; }// groups里面的存储的是模型数组 _groups=tempArray; } return_groups;}
-(void)viewDidLoad{ [superviewDidLoad]; //Doanyadditionalsetupafterloadingtheview,typicallyfromanib.// 创建表视图对象 UITableView*tabView=[[UITableViewalloc]initWithFrame:CGRectMake(0,20,375,657)style:UITableViewStylePlain]; // 数据源代理设置 tabView.dataSource=self; [self.viewaddSubview:tabView]; }//返回有多少组数据-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{ returnself.groups.count;}
//返回每组数据有多少条-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{// 拿到对应的组 CarGroups*group=self.groups[section]; // 返回每组有多少条数据 returngroup.cars.count;}-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{// 定义创建cel的标签 staticNSString*ID=@"car";// 在缓存池中查找对应标签的cel对象 UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:ID]; // 判断cell是否为空,是空的要创建 if(cell==nil){ cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ID]; } // 赋值给对应的row// 首先得到对应的车组 CarGroups*group=self.groups[indexPath.section]; CarDes*cars=group.cars[indexPath.row]; cell.textLabel.text=cars.name; cell.imageView.image=[UIImageimageNamed:cars.icon]; returncell; }
//返回组的名称-(NSString*)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)section{ CarGroups*group=self.groups[section]; returngroup.title;}/** *返回右边索引拦现实的字符串数组 */-(NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView{// kvc找不到键为title的值会自动到groups里面的元素找,找到对应的值,封装成数组返回,用valueForKey做不到 NSArray *array= [self.groupsvalueForKeyPath:@"title"]; return array;}@end
实现效果:
相关文章推荐
- UIAlertView的基本使用和对话框中按钮的事件处理方法
- 多线程NSInvocation+队列NSOperationQueue+NSBlockOperation
- likely(x)与unlikely(x)函数,即__builtin_expect的使用(转)
- 浅谈UitextField值变化的实时监视
- Codeforces 484B Maximum Value(排序+二分)
- [D3] 11. Basic D3 chart interactivity on(), select(this), classed(class, trueorfalse)
- Druid是什么
- Druid使用ConfigFilter
- 开发中使用到UITableView的各个技术点
- 如何在storyBoard中实现UIimageView长宽相等
- Java序列化中的serialVersionUID有什么用?
- iOS valueForKeyPath使用与感悟
- UITableView使用详解
- Reveal:分析iOS UI的利器
- UITableView_1简单使用
- MUI - 解决动态列表页图片懒加载再次加载不成功的bug
- UITableView优化
- poj 3384 Feng Shui(半平面交的联机算法)
- query_posts()的参数使用方法
- UIDevice的简易说明