本文介绍了在UITableViewCell上绘制垂直分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个普通样式的UITableView,它的单元格是UITableViewCell的子类.

I had a plain styled UITableView, and it's cells are a subclass of UITableViewCell.

在单元格子类中,我覆盖了drawRect以便将此绘图代码放入(用于垂直分隔符):

In the cell subclass I overrode drawRect to put this drawing code in (for a vertical separator):

CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(c, [[UIColor grayColor] CGColor]);
CGContextBeginPath(c);
CGContextMoveToPoint(c, self.frame.size.height + 0.5f, 0);
CGContextAddLineToPoint(c, self.frame.size.height + 0.5f, self.frame.size.height);
CGContextStrokePath(c);

效果很好.但是,现在我已将tableview样式更改为分组.根本没有画线.尽管设置了一个断点,但显示了drawRect方法被调用.

It worked great. However I have now changed the tableview style to grouped. The line simply isn't drawn. Although settings a breakpoint shows the drawRect method is called.

我想避免对UIView进行子类化,只是为了画一小段线,尤其是因为我已经对tableview单元格进行了子类化,而我只想在该单元格上进行画图.那么,为什么代码突然停止在分组表视图上工作?

I would like to avoid subclasses UIView just to draw a small line, especially as I've already subclassed the tableview cell and I just want to draw on the cell. So why does the code suddenly stop working on a grouped tableview?

推荐答案

UITableViewCell子类中覆盖drawRect并不是一个好主意.如果您不想设置自定义单元格的backgroundView,则只需添加宽度为1px的简单UIView.

It is not good idea to override drawRect in UITableViewCell subclasses. If you do not want to set custom cell's backgroundView, then you can just add simple UIView of 1px width.

UIView* vertLineView = [[UIView alloc] initWithFrame:CGRectMake(80, 0, 1, 44)];
vertLineView.backgroundColor = [UIColor redColor];
[self.contentView addSubview:vertLineView];

这篇关于在UITableViewCell上绘制垂直分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:25