如何访问tableView的标准viewForHeaderInSection? [英] How can I access the standard viewForHeaderInSection for a tableView?

查看:70
本文介绍了如何访问tableView的标准viewForHeaderInSection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有各个部分的索引UITableView。我想为每个部分中的标题视图使用不同的背景颜色。我知道我可以通过实现tableView来完成自己的视图:viewForHeaderInSection :(例如,参见问题#2898361 ),但这对我来说似乎是太多工作 - 标准视图看起来很好,我只需要改变它的背景颜色。

I've got an indexed UITableView with individual sections. I would like to use a different background color for the header views in each section. I know I can completely roll my own view by implementing tableView:viewForHeaderInSection: (for example, see question # 2898361), but that seems to be "too much work" to me - the standard view looks fine, I would just have to change its background color.

但是如何访问此标准视图?我不能使用 [super tableView:viewForHeaderInSection:] ,因为这是一个实现协议而不是继承问题的问题。我可以通过其他任何方式获得标准视图吗?

But how do I access this standard view? I can't use [super tableView:viewForHeaderInSection:] because this is a question of implementing a protocol and not an issue of inheritance. Any other way I can get the standard view?

推荐答案

我几乎可以肯定你不能轻易做到这一点。我最近在我的开发帐户上使用了我的技术支持请求之一,询问有关更改UITableView部分的背景和边框的问题。苹果工程师告诉我,这真的不是一件容易的事情,即使你设法做到了,你也可能会影响性能。他还向我指了cocoawithlove和一篇关于编辑uitableviews的文章:

I'm almost certain you can't do this easily. I used one of my tech support request on my dev account recently asking about altering the background and borders of UITableView sections. The apple engineer told me that this really wasn't an easy thing to do, and even if you managed to do it, you would probably affect performance. He also pointed me to cocoawithlove and an article about editing uitableviews:

http://cocoawithlove.com/2009/08/adding-shadow-effects-to-uitableview.html

真的,创建自己的标题并不是那么费力。下面是我从我的一个项目中提取的一些代码 - 它被注释掉了,所以可能不会马上工作 - 但你可以得到这个想法:

Really, creating your own header isn't that much effort. Below is some code I pulled out of one of my projects - it was commented out, so might not work straight away - but you can get the idea:

 - (CAGradientLayer *) greyGradient {
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.startPoint = CGPointMake(0.5, 0.0);
    gradient.endPoint = CGPointMake(0.5, 1.0);

    UIColor *color1 = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1.0];
    UIColor *color2 = [UIColor colorWithRed:240.0f/255.0f green:240.0f/255.0f blue:240.0f/255.0f alpha:1.0];

    [gradient setColors:[NSArray arrayWithObjects:(id)color1.CGColor, (id)color2.CGColor, nil]];
    return gradient;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CGFloat width = CGRectGetWidth(tableView.bounds); 
    CGFloat height = [self tableView:tableView heightForHeaderInSection:section];
    UIView *container = [[[UIView alloc] initWithFrame:CGRectMake(0,0,width,height)] autorelease];
    container.layer.borderColor = [UIColor grayColor].CGColor;
    container.layer.borderWidth = 1.0f;
    CAGradientLayer *gradient = [self greyGradient];
    gradient.frame = container.bounds;
    [container.layer addSublayer:gradient];

    UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(12,0,width,height)] autorelease];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.font= [UIFont boldSystemFontOfSize:19.0f];
    headerLabel.shadowOffset = CGSizeMake(1, 1);
    headerLabel.textColor = [UIColor whiteColor];
    headerLabel.shadowColor = [UIColor darkGrayColor];
    NSString *title = [self tableView:tableView titleForHeaderInSection:section];
    headerLabel.text = title;
    return container;
}

确保

#import <QuartzCore/QuartzCore.h>

顺便说一下......这不应该模仿标准标题的外观 - 它的只是一个例子。但我确定通过一些试验和错误你可以改变它来模仿标准的然后稍微改变颜色。

By the way... this isn't supposed to mimic the look of the standard headers - its just an example. But I'm sure with a bit of trial and error you could alter this to mimic the standard ones and then change the colors slightly.

这篇关于如何访问tableView的标准viewForHeaderInSection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆