我知道如何在单个 View 中绘制线条/矩形。但在这种情况下我迷路了:
我有一个 UIView
A 和一个 UITableView
B。A 被添加到 viewController 的 View ,B 被添加到 A。如您所知,UITableView
没有边界,因此我尝试沿着 B 的边界画线。
下面是我如何组织这些 View 的代码:
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
是继承自UIView
的ZJTransparencyView
实例,其实现文件如下:
- (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 没有作用。我不知道为什么会这样。
谁能帮帮我?
最佳答案
由于 tableRect 是您初始化表格的对象,因此您应该可以控制它。如果您认为没有刷新正确的矩形,请调用 setNeedsDisplay
没有指定矩形。但是,自 UITableView
以来,确实没有必要为此进行自定义绘图。继承自 UIView
你可以在UIView
周围画一个边框.
导入 QuartzCore
#import <QuartzCore/QuartzCore.h>
并将 UITableView 的边框设置为,
temp.layer.borderColor=[UIColor cyanColor].CGColor;
temp.layer.borderWidth=2;
关于ios - 如何在 UITableView 周围画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9783961/