UI控件 16UITable协议(2)

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值