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