1、在实现UITAbleView数据源协议即UITableViewDataSource内的如下方法中:
// 此方法用于告诉哪个UITableView的每一组的每一行显示什么内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
可以设置单元格的样式Style,当为如下所示
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];单元格的样式为Default时只显示一行文字。当它为别的样式例如:
UITableViewCellStyleSubtitle
UITableViewCellStyleValue1
UITableViewCellStyleValue2
样式时可以显示图片框,和两个文字Label。其中
cell.textLable.text = @“XXX是用来显示单元格图片框右侧最上面的大Label。而大Label下面小的Label则用如下方法进行设置,例如:cell.detailTextLabel.text =……
cell.imageView.iamge = [UIimage imageNameNamed:@“xxx.png”]; 用来设置ImageView的图片
2、 如何实现分组展示数据:
(1)因为默认是plain不分组,所以要设置UITableView的格式为分组格式。
(2)如何显示组标题
在遵守数据源协议的对象中添加如下方法并进行实现:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger) section;
——tableView:此参数是用来根据tag判断当前是哪个UITableView组件的。
——section:第几组。此参数是当前tag的UITableView组件的第几组,根据不同的section 即不同组分别返回不同的组标题。
(3)如何显示组描述
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
参数用法与以上相同,根据不同UITableView控件的不同组返回不同的组描述。
注意:在手动添加方法时有以下技巧:
(1)按住command键,进入相应协议寻找要添加的具体方法。
// 此方法用于告诉哪个UITableView的每一组的每一行显示什么内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
可以设置单元格的样式Style,当为如下所示
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];单元格的样式为Default时只显示一行文字。当它为别的样式例如:
UITableViewCellStyleSubtitle
UITableViewCellStyleValue1
UITableViewCellStyleValue2
样式时可以显示图片框,和两个文字Label。其中
cell.textLable.text = @“XXX是用来显示单元格图片框右侧最上面的大Label。而大Label下面小的Label则用如下方法进行设置,例如:cell.detailTextLabel.text =……
cell.imageView.iamge = [UIimage imageNameNamed:@“xxx.png”]; 用来设置ImageView的图片
2、 如何实现分组展示数据:
(1)因为默认是plain不分组,所以要设置UITableView的格式为分组格式。
(2)如何显示组标题
在遵守数据源协议的对象中添加如下方法并进行实现:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger) section;
——tableView:此参数是用来根据tag判断当前是哪个UITableView组件的。
——section:第几组。此参数是当前tag的UITableView组件的第几组,根据不同的section 即不同组分别返回不同的组标题。
(3)如何显示组描述
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
参数用法与以上相同,根据不同UITableView控件的不同组返回不同的组描述。
注意:在手动添加方法时有以下技巧:
(1)按住command键,进入相应协议寻找要添加的具体方法。
(2)先写出方法的返回类型,再进行实现。
代码验证示例如下:
编辑控制器的.h文件如下:
//
// ViewController.h
// UITableView展示分组数据
//
// Created by apple on 15/8/30.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@end
编辑控制器的.m文件如下:
//
// ViewController.m
// UITableView展示分组数据
//
// Created by apple on 15/8/30.
// Copyright (c) 2015年 LiuXun. All rights reserved.
//
#import "ViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped]; // 分配一个充满全屏,且为分组格式的UITableView空间
self.tableView.dataSource = self; // 为此UITableView控件设置数据源为当前控制器对象
[self.view addSubview:self.tableView]; // 将UITableView控件加到控制器的View中进行显示
}
// 以下实现数据源协议中的方法
// 方法一:告诉分为几个组
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 3; // 为UItableView组件分为3个组
}
// 方法二:每个组分为多少行
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) { //为第一组分配3行
return 3;
}else if(section == 1) // 为第二组分配2行
{
return 2;
}else{ // 为第三组分配1行
return 1;
}
}
// 方法三:告诉当前UITableView控件每一组的每一行单元格显示什么内容,返回一个单元格对象
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
if (indexPath.section == 0) { // 如果当前是第一组
if (indexPath.row == 0 ) { //如果为当前组的第一行
cell.textLabel.text = @"中国"; // 设置第一行的大标题
cell.detailTextLabel.text = @"历史悠久的国家"; // 设置第二行的小标题
cell.imageView.image = [UIImage imageNamed:@"7.png"]; // 设置ImageView
}else if (indexPath.row == 1){ // 如果为当前组的第二行
cell.textLabel.text = @"日本"; //设置上面的大标题
cell.detailTextLabel.text = @"非常小的国家"; // 设置下面的小标题
cell.imageView.image = [UIImage imageNamed:@"2.png"];
}else{ // 如果为当前组的第三行
cell.textLabel.text = @"朝鲜";
cell.detailTextLabel.text = @"非常小的国家"; // 设置下面的小标题
cell.imageView.image = [UIImage imageNamed:@"3.png"];
}
}else if (indexPath.section == 1){ // 如果当前是第二组
if (indexPath.row == 0 ) { //如果为当前组的第一行
cell.textLabel.text = @"美国";
cell.detailTextLabel.text = @"非常富有的国家"; // 设置下面的小标题
cell.imageView.image = [UIImage imageNamed:@"4.png"];
}else{ // 如果为当前组的第二行
cell.textLabel.text = @"英国";
cell.detailTextLabel.text = @"非常有文化的国家"; // 设置下面的小标题
cell.imageView.image = [UIImage imageNamed:@"5.png"];
}
}else{ //如果当前是第三组
cell.textLabel.text = @"东非";
cell.detailTextLabel.text = @"非常穷的国家"; // 设置下面的小标题
cell.imageView.image = [UIImage imageNamed:@"6.png"];
}
return cell;
}
// 方法四:为每一组添加组头(头标题)
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *title;
if (section == 0) { // 如果为当前UITableView组件的第一组
title = @"亚洲";
}else if (section == 1){// 如果为当前UITableView组件的第二组
title = @"欧洲";
}else{// 如果为当前UITableView组件的第三组
title = @"非洲";
}
return title;
}
// 方法五:为每一组添加组结尾描述
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
NSString *footer;
if (section == 0) { // 如果为当前UITableView组件的第一组
footer = @"亚洲是个人口众多的地方";
}else if (section == 1){// 如果为当前UITableView组件的第二组
footer = @"欧洲是个非常发达的地方";
}else{// 如果为当前UITableView组件的第三组
footer = @"非洲是个非常缺水的地方";
}
return footer;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
运行结果如下: