[英]UITableViewCell's subview not appearing
我的表格单元格可以扩展以显示一些子详细信息,并且我希望显示一个向下的三角形,以使用户知道这是可能的。
这是创建三角形视图的代码...
-(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;
因此,我想我对形状图层尚不了解,但是我在应用程序的另一部分使用了基本相同的绘图代码,因此效果很好。
点和矩形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;
}
也是这条线
self.openCloseTriangle = [self triangularViewWithWidth:100 height:100 pointsUp:NO];
这是有问题的:由于视图属于视图层次结构,因此视图属性通常应该较弱,但是如果将新创建的视图直接分配给此类属性,则可能会立即将其释放。 创建一个本地视图,将其添加到另一个视图,而不是将其分配给该属性,即弱视图。
#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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.