1.heighForRowAtIndexPath:获取单元格高度协议
2.heightForHeaderInSection:数据视图头部高度协议
3.heightForFooterInSection:数据视图尾部高度协议
4.titleForFooterInSection:数据视图尾部的标题协议
5.titleForHeaderInSection:数据视图头部标题协议
ViewController.h文件声明
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<
//数据代理协议
UITableViewDataSource,
//普通代理协议
UITableViewDelegate
>
{
//定义数据视图对象
UITableView * _tableView;
//声明一个数据源
NSMutableArray * _arrayData;
}
@end
ViewController.m文件实现:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//创建数据视图对象
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, 320, 536) style:UITableViewStyleGrouped];
//设置代理对象
_tableView.delegate = self;
//设置数据代理对象
_tableView.dataSource = self;
//数据视图显示
[self.view addSubview:_tableView];
//写到这里 由于协议没有实现 所以还不会显示数据
[_tableView release];
//分割线颜色
_tableView.separatorColor = [UIColor redColor];
//分割线类型
//_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//行高
_tableView.rowHeight = 100;
//背景颜色
_tableView.backgroundColor = [UIColor orangeColor];
//_tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"2_4.jpg"]];
//背景图片
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
imageView.image = [UIImage imageNamed:@"2_4.jpg"];
_tableView.backgroundView = imageView;
[imageView release];
//创建一个可变数组
_arrayData = [[NSMutableArray alloc]init];
for (int i = 'A'; i <= 'Z'; i++) {
//做一个二维数组 把字母分成6组 每组4个
NSMutableArray * arraySmall = [[NSMutableArray alloc]init];
for (int j = 1; j <= 5; j++) {
NSString * str = [NSString stringWithFormat:@"%c%d",i,j];
[arraySmall addObject:str];
}
//生成一个二维数组
[_arrayData addObject:arraySmall];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//返回二维数组一共有多少组
return _arrayData.count;
}
//获取每组元素个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//section:意思第几组
NSInteger numRow = [[_arrayData objectAtIndex:section] count];
return numRow;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * str = @"cell";
UITableViewCell * cell = [_tableView dequeueReusableCellWithIdentifier:str];
if ( cell == nil ){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
//cell选中效果
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = _arrayData[indexPath.section][indexPath.row];
return cell;
}
//单元格高度协议
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
//数据视图头部标题协议 获取显示在每一组头部的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"头部标题";
}
//数据视图尾部的标题协议 获取每一组尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @"尾部标题";
}
//数据视图头部高度协议 获取头部高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40;
}
//数据视图尾部高度协议 获取尾部高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 20;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end