码迷,mamicode.com
首页 > 移动开发 > 详细

iOS -- 汽车品牌(UITableView)

时间:2015-10-04 22:12:22      阅读:382      评论:0      收藏:0      [点我收藏+]

标签:

#import "MJViewController.h"
#import "MJCarGroup.h"
@interface MJViewController () <UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *carGroups;
@end
@implementation MJViewController
- (void)viewDidLoad{
[super viewDidLoad];
// 设置数据源
self.tableView.dataSource = self;
}
// 隐藏状态栏
- (BOOL)prefersStatusBarHidden{
return YES;
}
- (NSArray *)carGroups{
if (_carGroups == nil) {
// 初始化
// 德系品牌
MJCarGroup *cg1 = [[MJCarGroup alloc] init];
cg1.title = @"德系品牌";
cg1.desc = @"德系品牌很好";
cg1.cars = @[@"奥迪", @"宝马", @"奔驰", @"奥迪", @"宝马", @"奔驰", @"奥迪", @"宝马", @"奔驰", @"奥迪", @"宝马", @"奔驰"];
// 日韩品牌
MJCarGroup *cg2 = [[MJCarGroup alloc] init];
cg2.title = @"日韩品牌";
cg2.desc = @"日韩品牌很好haohaohao";
cg2.cars = @[@"本田", @"丰田", @"本田", @"丰田", @"本田", @"丰田", @"本田", @"丰田", @"本田", @"丰田", @"本田", @"丰田", @"本田", @"丰田", @"本田", @"丰田", @"本田", @"丰田"];
// 欧系品牌
MJCarGroup *cg3 = [[MJCarGroup alloc] init];
cg3.title = @"欧系品牌";
cg3.desc = @"欧系品牌goodgood";
cg3.cars = @[@"兰博基尼", @"法拉利"];
_carGroups = @[cg1, cg2, cg3];
}
return _carGroups;
}
#pragma mark - 数据源方法
// 一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.carGroups.count;
}
// 第section组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// 取得第section组对应的模型
MJCarGroup *cg = self.carGroups[section];
return cg.cars.count;
}
// 每一行显示怎样的内容(cell)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
// 取出第indexPath.section组对应的模型
MJCarGroup *cg = self.carGroups[indexPath.section];
// 取车第indexPath.row这行对应的品牌名称
NSString *car = cg.cars[indexPath.row];
// 设置cell显示的文字
cell.textLabel.text = car;
return cell;
}
// 第section组显示怎样的头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
MJCarGroup *cg = self.carGroups[section];
return cg.title;
}
// 第section组显示怎样的尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
MJCarGroup *cg = self.carGroups[section];
return cg.desc;
}
@end

iOS -- 汽车品牌(UITableView)

标签:

原文地址:http://www.cnblogs.com/lianfu/p/4855077.html

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