您的位置:首页 > 产品设计 > UI/UE

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文件

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

实现效果:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: