如何在表格标题区域viewForHeaderInSection中绘制线条和渐变? [英] How do I draw a line and a gradient in my table header section viewForHeaderInSection?

查看:52
本文介绍了如何在表格标题区域viewForHeaderInSection中绘制线条和渐变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UITableView标头部分添加了一些标题,我想在底部绘制一条白线,并在顶部至底部绘制一个灰色渐变.

I've added some titles in my UITableView header section and I'd like to draw a white line at the bottom and a a gray gradient going from top to bottom.

目前在viewForHeaderInSection中,我已经创建了带有标题标签的视图.我现在正在尝试画一条白线,我已经使用1像素高的标签进行了管理.

At the moment in viewForHeaderInSection I have create a view with labels for my headings. I'm now trying to draw a white line, which I've managed using a 1 pixel high label.

推荐答案

创建一个UIVIew子类,例如HeaderView,您将在其中绘制线条:

you show create an UIVIew subclass, say HeaderView in which you will draw your line:

@implementation HeaderView
- (void)drawRect:(CGRect)rect 
{
    [super drawRect:rect];

    //add a gradient:
    CAGradientLayer *layer = [[[CAGradientLayer alloc] init] autorelease]
    [gradientLayer setBounds:[self bounds]]
    [gradientLayer setColors:[NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil]];
    [[self layer] insertSublayer:gradientLayer atIndex:0];

    //draw line
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1); 
    CGContextMoveToPoint(ctx, 0, rect.size.height-1);
    CGContextAddLineToPoint( ctx, rect.size.width, rect.size.height-1);
    CGContextStrokePath(ctx);


}
@end

,然后在您的表委托中:

and then in your table delegate:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // create the parent view that will hold header Label
    HeaderView* customView = [[[HeaderView alloc] initWithFrame:CGRectMake(0.0, 0.0, 360.0, 20.0)] autorelease];

    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.frame = CGRectMake(10.0, 0.0, 100.0, 20.0); 
    headerLabel.text = [sectionTitles objectAtIndex:section];
    [customView addSubview:headerLabel];
    [headerLabel release];

    return customView;

}

这篇关于如何在表格标题区域viewForHeaderInSection中绘制线条和渐变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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