【问题标题】:how to draw line around a UITableView如何在 UITableView 周围画线
【发布时间】:2012-03-20 09:10:17
【问题描述】:

我知道如何在单个视图中绘制线条/矩形。但在这种情况下我迷路了: 我有一个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];

_transparencyViewZJTransparencyView的一个实例,继承自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);



}

我遇到的问题是: 当我添加表格视图并调用setNeedsDisplayInRect: 时,发送到drawRect: 的矩形始终为{0,0,320,460}。似乎 tableRect 没有效果。我不知道为什么会这样。 谁能帮帮我?

【问题讨论】:

    标签: ios uikit cgcontext


    【解决方案1】:

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

    导入 QuartzCore

    #import <QuartzCore/QuartzCore.h>
    

    并将 UITableView 的边框设置为,

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

    【讨论】:

    • 这就是我需要的。其实我也想用layer,但是没找到方法。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 2012-11-03
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多