Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在脚本中设计的自定义UITableViewCell中自定义UIView_Ios_Objective C_Uitableview_Uiview - Fatal编程技术网

Ios 在脚本中设计的自定义UITableViewCell中自定义UIView

Ios 在脚本中设计的自定义UITableViewCell中自定义UIView,ios,objective-c,uitableview,uiview,Ios,Objective C,Uitableview,Uiview,我正在尝试在自定义UITableViewCells中实现一些绘图。为此,我将自定义UIView子类添加到单元格中,并实现了自定义UIView子类的drawin-in-drawRect方法 以下是我的方法: 我有一个自定义UITableViewCells的UITableView。 我已经将UITableViewCell子类化,但设计直接在我的故事板中实现。单元格的内容是标准元素(UILabels和UIImageView)和一个自定义uiview子类 我已经使用约束和自动布局设置了故事板中的所有内容

我正在尝试在自定义UITableViewCells中实现一些绘图。为此,我将自定义UIView子类添加到单元格中,并实现了自定义UIView子类的drawin-in-drawRect方法

以下是我的方法:

我有一个自定义UITableViewCells的UITableView。 我已经将UITableViewCell子类化,但设计直接在我的故事板中实现。单元格的内容是标准元素(UILabels和UIImageView)和一个自定义uiview子类

我已经使用约束和自动布局设置了故事板中的所有内容

自定义uiview子类在drawRect方法中包含绘图代码

必须根据每个UITableViewCell的某些参数自定义图形。 问题是,定制UIView子类的正确方法是什么

现在我得到的只是一个黑色的立方体,我看不到任何图画。。。背景色不变,文本不显示(绘制)

编辑: 在cellForRowAtIndexPath中,我使用initWithText:andColor:方法创建自定义UIView:

- (instancetype)initWithText:(NSString *)text andColor:(UIColor *)color
{
    NSParameterAssert(text);
    self = [super init];
    if (self)
    {
        self.text = text;
        [self setBackgroundColor:color];
    }

    return self;
}


-(void)setBackgroundColor:(UIColor *)backgroundColor
{
    self.circleColor = backgroundColor;
    [super setBackgroundColor:[UIColor clearColor]];
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    [self.circleColor setFill];
    CGContextAddArc(context, CGRectGetMidX(rect), CGRectGetMidY(rect),
                CGRectGetWidth(rect)/2, 0, 2*M_PI, YES);
    CGContextFillPath(context);

}

- (void)drawSubtractedText:(NSString *)text inRect:(CGRect)rect
             inContext:(CGContextRef)context
{
    CGContextSaveGState(context);
    CGContextSetBlendMode(context, kCGBlendModeDestinationOut);

    CGFloat pointSize = 8;
    UIFont *font = [UIFont boldSystemFontOfSize:pointSize];

    CGContextTranslateCTM(context, 0,
                      (CGRectGetMidY(rect) - (font.lineHeight/2)));

    CGRect frame = CGRectMake(0, 0, CGRectGetWidth(rect), font.lineHeight);
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.font = font;
    label.text = text;
    label.textColor = [UIColor redColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor blueColor];
    [label.layer drawInContext:context];

    CGContextRestoreGState(context);
}

您应该在故事板中为标签和图像指定标记。使用这些标记并获得子视图的引用,如下所示

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
//some code
UILabel* cellDate = (UILabel *)[cellRef viewWithTag:1]
UIImageView* imageView = (UIImageView *)[cellRef viewWithTag:2]
//some code
}

你所做的是错误的。您已经在故事板
UITableviewCell
中添加了
UIView
参考。同样,您正在为
cellForRowIndexpath
方法中的相同自定义
UIView
类创建一个对象。您可以做的是,从故事板和
cellforrowindexpath
方法中删除
UIView
,如下所示

    CustomUIClass * c = [[CustomUIClass alloc] init];
    c.frame = CGRectMake(X, Y, Width, height);
    [cell.contentView addSubview:c];

在这里添加您自己的帧值以准备
CGRect

发布一些代码或图像,说明您正在做什么以及正在发生什么。这将导致我的cellForRowAtIndexPath函数膨胀。你认为在那里做自定义绘图是个好主意吗?谢谢。看起来是个好的开始。我试图绕过显式设置帧。这是否适用于约束?必须在cellForRowIndexpath方法中显式设置约束。或者,您可以做的是,让您的视图保留在序列图像板中,并将您在cellForRowIndexpath中创建的新视图添加到序列图像板视图中,而不是添加到上面指定的cell.contentView中。如果您遵循此方案,故事板的类名应该是UIView,而不是您的自定义视图.Thx。不幸的是,我最终得到了同一uiview的多个“层”。(多次创建同一个uiview,所有实例堆叠在一起)。我正在使用xcode的视图调试器检查视图层次结构。。。