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?
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
setNeedsDisplaywithout specifying rectangle. But, there really is no need to do custom drawing for this sinceUITableViewinherits fromUIViewand you can draw a border aroundUIView.Import QuartzCore
and set the border for the UITableView as,