Ios 在UITableViewCell中绘制线(不使用drawRect)
我有一个UITableViewCell,我想在其中画一些线(比如说,在距TableCell顶部1/3处画一条2pt线,覆盖整个宽度)。这些行将始终位于tableview单元格中的同一位置 一个简单的解决方案是使用Ios 在UITableViewCell中绘制线(不使用drawRect),ios,objective-c,uitableview,drawrect,Ios,Objective C,Uitableview,Drawrect,我有一个UITableViewCell,我想在其中画一些线(比如说,在距TableCell顶部1/3处画一条2pt线,覆盖整个宽度)。这些行将始终位于tableview单元格中的同一位置 一个简单的解决方案是使用-drawRect方法,该方法将使用CGContextStrokePath来画线。但这似乎有点过头了,因为它每次都会调用drawRect,这是低效的 我认为有一种方法可以以某种方式缓存它,这样所有的tableview单元格在默认情况下都是用行创建的。我能想到的一种方法是创建2pt*2pt
-drawRect
方法,该方法将使用CGContextStrokePath
来画线。但这似乎有点过头了,因为它每次都会调用drawRect,这是低效的
我认为有一种方法可以以某种方式缓存它,这样所有的tableview单元格在默认情况下都是用行创建的。我能想到的一种方法是创建2pt*2pt png文件,并将其添加到tableview单元格顶部的UIImageView中。还有其他方法吗?你可以只添加一个
UIView
像你的UITableViewCell
的子视图你可以只添加一个UIView
像你的UITableViewCell
的子视图你可以只添加一个UIView
像你的UITableViewCell
的子视图你可以只添加一个UIView
与UITableViewCell
的子视图类似,另一个选项是添加UIView
并设置其背景色。例如:
UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,cell.contentView.bounds.size.width, 2)] autorelease];
lineView.backgroundColor = [UIColor grayColor];
[cell.contentView addSubview:lineView];
另一个选项是,添加
ui视图
,并设置其背景色。例如:
UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,cell.contentView.bounds.size.width, 2)] autorelease];
lineView.backgroundColor = [UIColor grayColor];
[cell.contentView addSubview:lineView];
另一个选项是,添加
ui视图
,并设置其背景色。例如:
UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,cell.contentView.bounds.size.width, 2)] autorelease];
lineView.backgroundColor = [UIColor grayColor];
[cell.contentView addSubview:lineView];
另一个选项是,添加
ui视图
,并设置其背景色。例如:
UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,cell.contentView.bounds.size.width, 2)] autorelease];
lineView.backgroundColor = [UIColor grayColor];
[cell.contentView addSubview:lineView];
试试这个-
将此代码添加到您的单元格中,只需调整此图像视图的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];
试试这个-
将此代码添加到您的单元格中,只需调整此图像视图的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];
试试这个-
将此代码添加到您的单元格中,只需调整此图像视图的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];
试试这个-
将此代码添加到您的单元格中,只需调整此图像视图的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];
Ofc你也可以在故事板中这样做,所以所有单元格都有它(可重用单元格的性能应该比在代码中手动添加到每个单元格更好)Ofc你也可以在故事板中这样做,所以所有单元格都有它(可重用单元格的性能应该比在代码中手动添加到每个单元格更好)Ofc你也可以在故事板中这样做,所以所有单元格都有它(可重用单元格的性能应该比在代码中手动添加到每个单元格更好)Ofc你也可以在故事板中这样做,所以所有单元格都有它(可重用单元格的性能应该比在代码中手动添加到每个单元格更好)