在UITableViewCell中绘制线条(不使用drawRect)

时间:2013-11-22 08:49:37

标签: ios objective-c uitableview drawrect

我有一个UITableViewCell,其中我想画一些线条(比如在TableCell顶部的1/3覆盖整个宽度的2pt线)。这些行总是在tableview单元格中的相同位置。

一个简单的解决方案就是使用-drawRect方法,使用CGContextStrokePath绘制线条。但这看起来像是一种矫枉过正,因为它每次都会调用drawRect而效率低下。

我认为有一种方法能够以某种方式缓存,所以默认情况下所有tableview单元都是用行创建的。我能想到的一种方法是创建2pt * 2pt png文件并将其添加到tableview单元格顶部的UIImageView。还有其他方法吗?

3 个答案:

答案 0 :(得分:2)

试试这个 -

cellForRowAtIndexPath中添加此代码,只需调整y位置即可显示此图片视图 -

UIImageView *seperatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 49, 320, 1)];
seperatedImageView.backgroundColor = [UIColor colorWithRed:201.0/255.0 green:250.0/255.0 blue:152.0/255.0 alpha:1.0];
[cellBackView addSubview:seperatedImageView];

答案 1 :(得分:1)

另一种选择是添加UIView并设置其背景颜色。例如:

 UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,cell.contentView.bounds.size.width, 2)] autorelease];
 lineView.backgroundColor =  [UIColor grayColor];

 [cell.contentView addSubview:lineView];

答案 2 :(得分:0)

您只需添加UIView,就像UITableViewCell

的子视图一样