码迷,mamicode.com
首页 > 其他好文 > 详细

12-26 汽车品牌小应用

时间:2015-12-26 18:41:47      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

一: 流程: 1.通过plist加载模型对象,代码如下: #import "CZCarGroup.h" @implementation CZCarGroup // 从字典里采的数据赋给这个对象 - (instancetype) initWithDic:(NSDictionary *)dic { if (self == [super init]) { [self setValuesForKeysWithDictionary:dic]; } return self; } //实例化一个对象,调用的是类方法 + (instancetype) carGroupWithDic:(NSDictionary *)dic { // 生成一个实例化的类对象,并且利用对象方法从dic里传递数据给他; return [[self alloc] initWithDic:dic]; } // 最终调用的方法,直接生成从support文件中采的数据的CZCarGroup对象 + (NSArray *)carGroupsList { NSString *path = [[NSBundle mainBundle ] pathForResource:@"cars_simple" ofType:@"plist"]; NSArray *dicArray = [NSArray arrayWithContentsOfFile:path]; NSMutableArray *tmpArray = [NSMutableArray array]; for (NSDictionary *dic in dicArray) { CZCarGroup *carGroup = [CZCarGroup carGroupWithDic:dic]; [tmpArray addObject:carGroup]; } return tmpArray; } @end 2.在controller中添加属性,并加载数据,代码如下:(使用懒加载) //1.实现懒加载 - (NSArray *)carGroups { if(_carGroups == nil) { _carGroups = [CZCarGroup carGroupsList]; } return _carGroups; } 3.拖拽一个tableView(使用tableView必须设置数据源获取数据),代码如下: - (void)viewDidLoad { [super viewDidLoad]; // 2.设置数据源 self.tableView.dataSource = self; // 测试数据有没有加载进来 NSLog(@"%@",self.carGroups); } 4.让controller遵守数据源协议 @interface ViewController () 5.在viewIDidLoad中,设置tableView的数据源为controller - (void)viewDidLoad { [super viewDidLoad]; // 2.设置数据源 self.tableView.dataSource = self; // 测试数据有没有加载进来 NSLog(@"%@",self.carGroups); } 6.实现数据源方法 //3.实现数据源方法 #pragma mark - 数据源方法 //共有多少组数据 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.carGroups.count; } //每一行有多少组 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { CZCarGroup * carGroup = self.carGroups[section]; return carGroup.cars.count ; } //设定每个cell中显示的内容 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 1.创建一个UITableViewCell UITableViewCell *cell = [ [UITableViewCell alloc] init]; // 2.获得当前组的数据 CZCarGroup *cargroup = self.carGroups[indexPath.section]; cell.textLabel.text = cargroup.cars[indexPath.row]; // 返回cell return cell; } //设置尾部标示 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { CZCarGroup * carGroup = self.carGroups[section]; return carGroup.title; } //设置头部标示 - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { CZCarGroup * carGroup = self.carGroups[section]; return carGroup.desc; } 7.演示效果

12-26 汽车品牌小应用

标签:

原文地址:http://www.cnblogs.com/BJTUzhengli/p/5078520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!