#import "ViewController.h"
#import "XMGCar.h"
#import "XMGCarGroup.h"
@interface ViewController () <UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSArray *groups ;
@end
@implementation ViewController
- (NSArray *)groups
{
if (_groups == nil) {
// 创建模型数据
XMGCarGroup *group0 = [[XMGCarGroup alloc] init];
group0.header = @"德系";
group0.footer = @"德系车666";
group0.cars = @[
[XMGCar carWithName:@"奥迪" icon:@"m_9_100"],
[XMGCar carWithName:@"宝马" icon:@"m_3_100"],
[XMGCar carWithName:@"奔驰" icon:@"m_2_100"]
];
XMGCarGroup *group1 = [[XMGCarGroup alloc] init];
group1.header = @"日系";
group1.footer = @"日系车666777";
group1.cars = @[
[XMGCar carWithName:@"奥迪1" icon:@"m_9_100"],
[XMGCar carWithName:@"奔驰1" icon:@"m_2_100"],
[XMGCar carWithName:@"奥迪1" icon:@"m_9_100"],
[XMGCar carWithName:@"奔驰1" icon:@"m_2_100"],
[XMGCar carWithName:@"奥迪1" icon:@"m_9_100"],
[XMGCar carWithName:@"奔驰1" icon:@"m_2_100"],
[XMGCar carWithName:@"奥迪1" icon:@"m_9_100"],
[XMGCar carWithName:@"奔驰1" icon:@"m_2_100"],
[XMGCar carWithName:@"奥迪1" icon:@"m_9_100"],
[XMGCar carWithName:@"奔驰1" icon:@"m_2_100"],
[XMGCar carWithName:@"奥迪1" icon:@"m_9_100"],
[XMGCar carWithName:@"奔驰1" icon:@"m_2_100"]
];
XMGCarGroup *group2 = [[XMGCarGroup alloc] init];
group2.header = @"日系1";
group2.footer = @"日系车999999";
group2.cars = @[
[XMGCar carWithName:@"奥迪2" icon:@"m_9_100"],
[XMGCar carWithName:@"奔驰2" icon:@"m_2_100"]
];
_groups = @[group0, group1, group2];
}
return _groups;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 设置数据源
self.tableView.dataSource = self;
}
#pragma mark - <UITableViewDataSource>
/**
* 告诉tableView第section组有多少行
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
XMGCarGroup *group=self.groups[section];
return group.cars.count;
}
/**
* 告诉tableView一共有多少组数据
*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.groups.count;
}
/**
* 告诉tableView第indexPath行显示怎样的cell
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] init];
return cell;
XMGCarGroup *group=self.groups[indexPath.section];
XMGCar *car=group.cars[indexPath.row];
cell.textLabel.text=car.name;
cell.imageView.image=[UIImage imageNamed:car.icon];
return cell;
}
/**
* 告诉tableView第section组的头部标题
*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
XMGCarGroup *group = self.groups[section];
return group.header;
}
/**
* 告诉tableView第section组的尾部标题
*/
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
XMGCarGroup *group = self.groups[section];
return group.footer;
}
@end
UITableView多组数据
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 在解决了cell重用问题后,在输入文字后 在将界面向下拉,再重新拉回来的时候就会发现数据没了 这时候咱们可以在输完...
- A drop-in UITableView/UICollectionView superclass categor...
- 包含组件 最底层File System -> SQLite -> NSPersistent Store(可有多个)...