ios - UITableViewCell 的 subview 没有出现

标签 ios objective-c cocoa-touch core-animation

我有可以展开以显示一些子详细信息的表格单元格,我希望出现一个向下的三角形,让用户知道这是可能的。

这是创建三角形 View 的代码...

-(UIView *)triangularViewWithWidth:(CGFloat)width height:(CGFloat)height pointsUp:(BOOL)pointsUp {
UIBezierPath *trianglePath = [UIBezierPath bezierPath];
if(pointsUp){
    [trianglePath moveToPoint:CGPointMake(width / 2, 0)];
    [trianglePath moveToPoint:CGPointMake(width, height)];
    [trianglePath moveToPoint:CGPointMake(0, height)];
    [trianglePath closePath];
} else {
    [trianglePath moveToPoint:CGPointMake(0, 0)];
    [trianglePath moveToPoint:CGPointMake(width, 0)];
    [trianglePath moveToPoint:CGPointMake(width / 2, height)];
    [trianglePath closePath];
}
CAShapeLayer *l = [CAShapeLayer layer];
l.path = trianglePath.CGPath;

UIView *triangleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
triangleView.layer.mask = l;
return triangleView;
}

以及实现它的代码...

-(void)drawTriangle {
if([self.detail.subDetails count]){
    self.openCloseTriangle = [self triangularViewWithWidth:100 height:100 pointsUp:NO];
    NSLog(@"triangle origin is: %@", NSStringFromCGPoint(self.openCloseTriangle.frame.origin));
    NSLog(@"triangle size is: %@", NSStringFromCGSize(self.openCloseTriangle.frame.size));
    self.openCloseTriangle.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.openCloseTriangle];
    [self.contentView bringSubviewToFront:self.openCloseTriangle];
}
}

如果我注释掉这一行,代码将按预期工作(创建一个大的红色矩形):

l.path = trianglePath.CGPath;

所以我想我对形状图层有些不了解,但我在我的应用程序的另一部分使用了基本相同的绘图代码并且它工作正常。

point 和 rect NSLog 输出检查也很好(即使它没有出现):

三角形原点是:{0, 0} 三角形大小为:{100, 100}

最佳答案

您必须使用 addLineToPoint: 而不是 moveToPoint: 来创建路径:

-(UIView *)triangularViewWithWidth:(CGFloat)width height:(CGFloat)height pointsUp:(BOOL)pointsUp {
    UIBezierPath *trianglePath = [UIBezierPath bezierPath];
    if(pointsUp){
        [trianglePath moveToPoint:CGPointMake(width / 2, 0)];
        [trianglePath addLineToPoint:CGPointMake(width, height)];
        [trianglePath addLineToPoint:CGPointMake(0, height)];
        [trianglePath closePath];
    } else {
        [trianglePath moveToPoint:CGPointMake(0, 0)];
        [trianglePath addLineToPoint:CGPointMake(width, 0)];
        [trianglePath addLineToPoint:CGPointMake(width / 2, height)];
        [trianglePath closePath];
    }
    CAShapeLayer *l = [CAShapeLayer layer];
    l.path = trianglePath.CGPath;

    UIView *triangleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    triangleView.layer.mask = l;    
    return triangleView;
}

red triangle


还有这一行

self.openCloseTriangle = [self triangularViewWithWidth:100 height:100 pointsUp:NO];

是有问题的: View 属性通常应该是弱的,因为 View 由 View 层次结构拥有,但是如果您将新创建的 View 直接分配给这样的属性,它可能会立即被释放。创建一个本地 View ,将其添加到另一个 View ,而不是将其分配给弱的属性。


我的测试代码

#import "ViewController.h"

@interface ViewController ()
@property (weak) UIView *openCloseTriangle;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self drawTriangle];
}

-(UIView *)triangularViewWithWidth:(CGFloat)width height:(CGFloat)height pointsUp:(BOOL)pointsUp {
    UIBezierPath *trianglePath = [UIBezierPath bezierPath];
    if(pointsUp){
        [trianglePath moveToPoint:CGPointMake(width / 2, 0)];
        [trianglePath addLineToPoint:CGPointMake(width, height)];
        [trianglePath addLineToPoint:CGPointMake(0, height)];
        [trianglePath closePath];
    } else {
        [trianglePath moveToPoint:CGPointMake(0, 0)];
        [trianglePath addLineToPoint:CGPointMake(width, 0)];
        [trianglePath addLineToPoint:CGPointMake(width / 2, height)];
        [trianglePath closePath];
    }
    CAShapeLayer *l = [CAShapeLayer layer];
    l.path = trianglePath.CGPath;

    UIView *triangleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    triangleView.layer.mask = l;    
    return triangleView;
}


-(void)drawTriangle {
    UIView * v= [self triangularViewWithWidth:100 height:100 pointsUp:NO];
    self.openCloseTriangle = v;
    NSLog(@"triangle origin is: %@", NSStringFromCGPoint(self.openCloseTriangle.frame.origin));
    NSLog(@"triangle size is: %@", NSStringFromCGSize(self.openCloseTriangle.frame.size));
    self.openCloseTriangle.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.openCloseTriangle];
}

@end

关于ios - UITableViewCell 的 subview 没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26982950/

相关文章:

iphone - 在 pushViewController 动画期间避免 UIBarButtonItem 淡出/淡入

ios - 在左上角创建自定义圆角矩形按钮角?

ios - 如何从 Firebase 的自定义类函数中调用数据

ios - 使用在 swift 中的 xib 中创建的自定义 tableViewCells

iphone - 需要释放 IBOutlet 对象?

iphone - 想使用 popViewControllerAnimated 而不是 popToRootViewControllerAnimated

objective-c - 如何重新创建 Jetsetter 的 iOS 预告片照片和照片查看器幻灯片过渡?

ios - 处理 IAP(应用内购买)订阅计划的正确方法是什么

ios - 如何根据IOS中的时间更改十六进制颜色代码

objective-c - 具有核心数据和表格 View 的 Apple 模板 iOS 文件