如何在UITableView周围绘制线条

时间:2012-03-20 09:10:18

标签: ios uikit cgcontext

我知道如何在单个视图中绘制直线/矩形。但在这种情况下,我迷失了方向: 我有一个UIView A和一个UITableView B. A添加到viewController的视图中,B添加到A.如您所知,UITableView没有边框,因此我尝试沿着B的边界绘制线条。 以下是我组织这些视图的代码:


if (_tableView) {
        [_tableView release];
        _tableView = nil;
    }


    UITableView * temp = [[UITableView alloc] initWithFrame:tableRect style:UITableViewStylePlain];
    temp.delegate = self;
    temp.dataSource = self;
    temp.showsHorizontalScrollIndicator = NO;
    temp.alwaysBounceHorizontal = NO;

    self.table = temp;
    [temp release];
    temp = nil;
    [self.table setContentInset:UIEdgeInsetsMake(1, 2, 0, 2)];

    [_transparencyView addSubview:self.table];


    [self.parentViewController.view addSubview:_transparencyView];
    [_transparencyView setNeedsDisplayInRect:tableRect];

_transparencyView是从ZJTransparencyView继承的UIView的实例,其实现文件如下:


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    NSLog(@"%s:%@",__func__,NSStringFromCGRect(rect));

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor cyanColor].CGColor);
    CGContextSetLineWidth(context, 2);
    CGContextStrokeRect(context, rect);



}

我遇到的问题是: 当我添加tableview并调用setNeedsDisplayInRect:时,发送到drawRect:的rect始终为{0,0,320,460}。似乎tableRect没有效果。我不知道为什么会这样。 有谁可以帮助我?

1 个答案:

答案 0 :(得分:2)

由于tableRect是初始化表的内容,因此您应该对其进行控制。如果您认为未刷新正确的矩形,请在不指定矩形的情况下调用setNeedsDisplay。但是,由于UITableView继承自UIView并且您可以在UIView周围绘制边框,因此确实无需为此进行自定义绘图。

导入QuartzCore

#import <QuartzCore/QuartzCore.h>

并将UITableView的边框设置为

temp.layer.borderColor=[UIColor cyanColor].CGColor;
temp.layer.borderWidth=2;