在 Storyboard 中设计的自定义 UITableViewCell 中的自定义 UIView
Posted
技术标签:
【中文标题】在 Storyboard 中设计的自定义 UITableViewCell 中的自定义 UIView【英文标题】:Custom UIView in Custom UITableViewCell designed in Storyboard 【发布时间】:2016-01-19 09:34:54 【问题描述】:我正在尝试在自定义 UITableViewCells 中实现一些绘图。为此,我在单元格中添加了自定义 UIView 子类,并在自定义 UIView 子类的 drawRect 方法中实现了绘图。
这是我的方法:
我有一个带有自定义 UITableViewCells 的 UITableView。 我将 UITableViewCell 子类化,但设计直接在我的故事板中实现。单元格的内容是标准元素(UILabels 和 UIImageViews)和一个自定义 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);
【问题讨论】:
张贴一些代码或图像,说明您正在做什么以及正在发生的事情。 【参考方案1】:您会在情节提要中为标签和图像指定标签。使用这些标签并获取子视图的引用,如下所示
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//some code
UILabel* cellDate = (UILabel *)[cellRef viewWithTag:1]
UIImageView* imageView = (UIImageView *)[cellRef viewWithTag:2]
//some code
【讨论】:
这最终会使我的 cellForRowAtIndexPath 函数膨胀。您认为在其中进行自定义绘图是个好主意吗?【参考方案2】:你做错了。您已经在故事板UITableviewCell
中添加了UIView
参考。再次,您正在为cellForRowIndexpath
方法中的同一自定义UIView
类创建一个对象。您可以做的是,从情节提要和cellforrowindexpath
方法中删除UIView
,如下所示。
CustomUIClass * c = [[CustomUIClass alloc] init];
c.frame = CGRectMake(X, Y, Width, height);
[cell.contentView addSubview:c];
这里添加你自己的帧值准备CGRect
。
【讨论】:
谢谢。看起来是一个好的开始。我试图绕过明确设置一个框架。这是否适用于约束? 您必须在 cellForRowIndexpath 方法中显式设置约束。或者,您可以做的是,让您的视图保留在情节提要中,并在 cellForRowIndexpath 中创建新视图,将其添加到情节提要视图中,而不是添加到上面指定的 cell.contentView 中。如果您遵循这种情况,情节提要的类名应该是 UIView 而不是您的自定义视图。 再次感谢。不幸的是,我最终得到了同一个 uiview 的多个“层”。 (多次创建相同的 uiview,所有实例堆叠在一起)。我正在使用 xcode 的视图调试器检查视图层次结构...以上是关于在 Storyboard 中设计的自定义 UITableViewCell 中的自定义 UIView的主要内容,如果未能解决你的问题,请参考以下文章