Iphone 如何在表格标题部分视图ForHeaderInSection中绘制直线和渐变?

Iphone 如何在表格标题部分视图ForHeaderInSection中绘制直线和渐变?,iphone,objective-c,xcode,uitableview,Iphone,Objective C,Xcode,Uitableview,我在UITableView标题部分添加了一些标题,我想在底部画一条白线,并从上到下画一个灰色渐变 目前在viewForHeaderInSection中,我已经创建了一个带有标题标签的视图。我现在正在尝试绘制一条白线,我使用了一个1像素高的标签来管理这条白线。您将演示如何创建一个UIVIew子类,比如HeaderView,您将在其中绘制线: @implementation HeaderView - (void)drawRect:(CGRect)rect { [super drawRect

我在UITableView标题部分添加了一些标题,我想在底部画一条白线,并从上到下画一个灰色渐变


目前在
viewForHeaderInSection
中,我已经创建了一个带有标题标签的视图。我现在正在尝试绘制一条白线,我使用了一个1像素高的标签来管理这条白线。

您将演示如何创建一个UIVIew子类,比如HeaderView,您将在其中绘制线:

@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
然后在您的表中,委派:

- (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;

}

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

@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
然后在您的表中,委派:

- (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;

}不想对视图(UILabel/UIButton)进行子类化的人


不希望对视图(UILabel/UIButton)进行子类化的人员等


我知道这个问题已经得到了回答,但我在让线划过坡度时遇到了问题。我想可能还有其他人也有同样的问题,所以这里是我如何解决它的。这适用于iOS 4x

CustomTableViewSectionHeaderWithLine.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface CustomTableViewSectionHeaderWithLine : UIView {
    UIColor *topColor;
    UIColor *bottomColor;
    UIColor *lineColor;
}
@property(nonatomic, retain) UIColor *topColor, *bottomColor, *lineColor;
@end
用法(在TableViewDelegate中实现这两种方法

- (UIView *) tableView:(UITableView *) tableView viewForHeaderInSection:(NSInteger)section {
    CustomTableViewSectionHeaderWithLine *customView = [[[CustomTableViewSectionHeaderWithLine alloc] initWithFrame:CGRectMake(0.0, 0.0, 360.0, 25.0)] autorelease];
    [customView setTopColor:[UIColor whiteColor]];
    [customView setBottomColor:[UIColor blackColor]];
    [customView setLineColor:[UIColor whiteColor]];

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 18)] autorelease];
    label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section];
    label.textColor = [UIColor blackColor];
    label.backgroundColor = [UIColor clearColor];
    [customView addSubview:label];

    return customView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 25;
}

我知道这个问题已经得到了回答,但我在绘制渐变线时遇到了一些问题。我想可能还有其他人也有同样的问题,所以下面是我解决问题的方法。这适用于iOS 4x

CustomTableViewSectionHeaderWithLine.h

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface CustomTableViewSectionHeaderWithLine : UIView {
    UIColor *topColor;
    UIColor *bottomColor;
    UIColor *lineColor;
}
@property(nonatomic, retain) UIColor *topColor, *bottomColor, *lineColor;
@end
用法(在TableViewDelegate中实现这两种方法

- (UIView *) tableView:(UITableView *) tableView viewForHeaderInSection:(NSInteger)section {
    CustomTableViewSectionHeaderWithLine *customView = [[[CustomTableViewSectionHeaderWithLine alloc] initWithFrame:CGRectMake(0.0, 0.0, 360.0, 25.0)] autorelease];
    [customView setTopColor:[UIColor whiteColor]];
    [customView setBottomColor:[UIColor blackColor]];
    [customView setLineColor:[UIColor whiteColor]];

    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 18)] autorelease];
    label.text = [tableView.dataSource tableView:tableView titleForHeaderInSection:section];
    label.textColor = [UIColor blackColor];
    label.backgroundColor = [UIColor clearColor];
    [customView addSubview:label];

    return customView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 25;
}

Romain,请看上面的编辑,我对你的代码做了一些小的颜色更改。但是,渐变超出了标题区域?并且我无法将线条设置为白色。我添加了一个高度为1的标签,这是最后一张图像,因此我知道灰线不是高度1。CoreGraphics将始终绘制像素边界,这意味着如果如果你有一条单像素线,它将在两个像素之间绘制,使它看起来像一条粗粗的2像素线,颜色已褪色。对于水平线,你需要将Y坐标偏移0.5,对于垂直线,则需要将Y坐标偏移0.5。我需要进行校正
[gradientLayer setFrame:rect]
在用水晶头骨修复我的底线后,我的底线被隐藏在渐变后面。所以我坚持使用我的单像素标签。一切看起来都很好:)我不得不一层一层地修改第一个渐变层,还有Jules所做的修改。Romain,请看上面的编辑,我对你的代码做了一些小的颜色修改。但是,渐变超出了标题区域?我无法使线条变白。我添加了一个高度为1的标签,这是最后一幅图像,所以我知道灰线不是高度1.CoreGraphics将始终跨像素边界绘制,这意味着如果您有一条单像素线,它将在两个像素之间绘制,使其显示为一条粗粗的、有褪色颜色的2像素线。对于水平线,您需要将Y坐标偏移0.5,对于垂直线,则需要将Y坐标偏移0.5。我需要制作一个correction
[gradientLayer setFrame:rect];
在用水晶头骨固定我的底线后,我的底线被隐藏在渐变后面。因此我坚持使用单像素标签。所有看起来都很好:)我不得不一层一层地更改第一个渐变层,以及朱尔斯所做的更改。