scorpiozj
scorpiozj

Reputation: 2687

how to draw line around a UITableView

I know how to draw lines/rect in a single view. But I'm lost in this case: I have a UIView A and a UITableView B. A is added to a viewController's view, and B is added to A. As You know, UITableView had no border and hence I try to draw line along the bounds of the B. Below is the code how I organize these views:


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

The _transparencyView is an instance of ZJTransparencyView inherited from UIView, and its implement file is like:


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



}

The problem I met is : When I add the tableview and call setNeedsDisplayInRect:, the rect send to drawRect: is always {0,0,320,460}. It seems that tableRect has no effect. I don't know why it happens. Could anyone help me?

Upvotes: 1

Views: 916

Answers (1)

MadhavanRP
MadhavanRP

Reputation: 2830

Since tableRect is what you initialize the table, you should have control over it. If you think that the correct rectangle is not refreshed, call setNeedsDisplay without specifying rectangle. But, there really is no need to do custom drawing for this since UITableView inherits from UIView and you can draw a border around UIView.

Import QuartzCore

#import <QuartzCore/QuartzCore.h>

and set the border for the UITableView as,

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

Upvotes: 2

Related Questions