- #pragma mark - UITableViewDataSource
- /**
- * 1. 告诉 tableview 一共有多少组
- */
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- NSLog(@"numberOfSectionsInTableView");
- return 2;
- }
- /**
- * 2. 第 section 组有多少行
- */
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- NSLog(@"numberOfRowsInSection %d", section);
- if (0 == section) {
- // 第 0 组有多少行
- return 2;
- }else
- {
- // 第 1 组有多少行
- return 3;
- }
- }
- /**
- * 3. 告知系统每一行显示什么内容
- */
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSLog(@"cellForRowAtIndexPath %d %d", indexPath.section, indexPath.row);
- // indexPath.section; // 第几组
- // indexPath.row; // 第几行
- // 1. 创建 cell
- UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
- // 2. 设置数据
- // cell.textLabel.text = @"汽车";
- // 判断是第几组的第几行
- if (0 == indexPath.section) {
- // 第 0 组
- if (0 == indexPath.row) // 第 0 组第 0 行
- {
- cell.textLabel.text = @"奥迪";
- }else if (1 == indexPath.row) // 第 0 组第 1 行
- {
- cell.textLabel.text = @"宝马";
- }
- }else if (1 == indexPath.section) // 第 1 组
- {
- if (0 == indexPath.row) {
- // 第 0 组第 0 行
- cell.textLabel.text = @"本田";
- }else if (1 == indexPath.row) // 第 0 组第 1 行
- {
- cell.textLabel.text = @"丰田";
- }else if (2 == indexPath.row) // 第 0 组第 2 行
- {
- cell.textLabel.text = @"马自达";
- }
- }
- // 3. 返回要显示的控件
- return cell;
- }
- /**
- * 第 section 组头部显示什么标题
- *
- */
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
- {
- // return @"标题";
- if (0 == section) {
- return @"德系品牌";
- }else
- {
- return @"日韩品牌";
- }
- }
- /**
- * 第 section 组底部显示什么标题
- *
- */
- - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
- {
- if (0 == section) {
- return @"高端大气上档次";
- }else
- {
- return @"还不错";
- }
- }
- @end
来源: http://www.bubuko.com/infodetail-2775826.html