ios - 如何在 UITableView 周围画线

标签 ios uikit cgcontext

我知道如何在单个 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是继承自UIViewZJTransparencyView实例,其实现文件如下:


- (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/

相关文章:

ios - 基于背景点击关闭 View

ios - 带套接字的应用程序,显示警报返回到根 Controller

Iphone CGContextShowTextAtPoint 对于日语字符

iphone - 当应用程序在后台时,每 5 分钟通过 iPhone 应用程序向服务器发送位置更新

ios - ImageView 动画重复

swift - iPhone X、iPhone XS 等相机变焦问题

iphone - 你如何在 NSUserDefaults 中存储多个 UIControlState 实例?

objective-c - 使用贝塞尔路径

objective-c - iOS:在 Canvas 上动态绘图(上下文)

ios - 为 CoreText 创建 CGContext