表的组头尾视图的自定义

AppDelegate设置根视图控制器

RootViewController.h

@interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *_data;
}

RootViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    //创建表视图
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, 460) style:UITableViewStyleGrouped];

    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
    [tableView release];
    
    <span style="color:#ff0000;">//读取本地的数据</span>
    NSString *path = [[NSBundle mainBundle] pathForResource:@"font" ofType:@"plist"];
    _data = [[NSArray alloc] initWithContentsOfFile:path];
    
}

#pragma mark - UITableView dataSource
//指定组的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _data.count;
}

//指定相应组的cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSArray *arrary2D = [_data objectAtIndex:section];
    return arrary2D.count;
}

//创建单元格cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    NSArray *arrary2D = [_data objectAtIndex:indexPath.section];
    NSString *str = [arrary2D objectAtIndex:indexPath.row];
    
    cell.textLabel.text = str;
    cell.textLabel.font = [UIFont fontWithName:str size:17];
    
    return [cell autorelease];
    
}

#pragma mark - UITableViewDelegate
//单元格被选中后调用的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%d组%d行的单元格被选中了",indexPath.section,indexPath.row);
    <span style="color:#ff0000;">//取消选中效果</span>
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


/*
 下面这个方法
 */
//给组的头视图设置标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *str = [NSString stringWithFormat:@"头视图的标题:%d组的标题",section];
    return str;
}
//给组的尾视图设置标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {

    NSString *str = [NSString stringWithFormat:@"尾视图的标题:%d组的标题",section];
    return str;
    
}

//自定义组的头视图和尾视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

   <span style="background-color: rgb(255, 0, 0);"> //headerView的frame值设置没有效果</span>
    //CGRectMake(0, 0, 0, 0)等价于CGRectZero
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectZero]autorelease];
    headerView.backgroundColor = [UIColor redColor];
    
    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 10, 180, 20)]autorelease];
    label.text = [NSString stringWithFormat:@"尾视图的标题:%d组的标题",section];
    label.textColor = [UIColor greenColor];
    label.font = [UIFont systemFontOfSize:15];
    [headerView addSubview:label];

    return headerView;
}
//设置组的头视图的高度
//或者    tableView.sectionHeaderHeight
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值