前言
对于iOS的tableView的cell的分割线,一般我们很少使用不是系统默认的,但是有些项目要求还是要求我们去改变分割线的颜色或者外形以配合整个项目的色调。这个苹果公司早都为我们想到了。
一、关于分割线的位置。
分割线的位置就是指分割线相对于tableViewCell.如果我们要根据要求调节其位置,那么在iOS7.0版本以后,提供了一个方法如下:
1
2
3
4
5
|
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 45, 0, 0)];
}
|
UIEdgeInsets 的四个参数分别是相对于cell的上、左、下、右的距离,都是CGFloat型。
二、分割线的颜色及风格:
a、cell的分割线的颜色不是cell的属性,它属于tableView的separatorColor属性。这样我们只需要设置属性值就可以得到所有我们想要的颜色的分割线、
1
|
[self.tableView setSeparatorColor:[UIColor clearColor]];
|
b、cell的风格:它是tableView 的separatorStyle属性,系统给我们提供了三种风格在枚举UITableViewCellSeparatorStyle中定义,分别是
1
2
3
4
5
|
typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine,
UITableViewCellSeparatorStyleSingleLineEtched // This separator style is only supported for grouped style table views currently
};
|
默认的是UITableViewCellSeparatorStyleSingleLine.
三、tableViewCell 分割线自定义
首先要把cell自带的分割线给去掉,使用如下两种都行,一是把颜色设置为clearColor,二是风格设置为UITableViewCellSeparatorStyleNone。
a、把自定义的分割线当成一个View放到cell的contentView上,一定要注意重用问题,所以这个view 要在cell初始化的时候添加上。示例代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@ "cell" ];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@ "cell" ];
cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@ "huicellacce" ]];
cell.backgroundColor = [UIColor clearColor];
// cell.selected = YES;
UIImageView *imageViewSepE = [[UIImageView alloc]initWithFrame:CGRectMake(47, 49, 200, 1)];
imageViewSepE.image = [UIImage imageNamed:@ "godline" ];
[cell.contentView addSubview:imageViewSepE];
}
}
|
b、比较复杂,用到了底层的框架,
1
2
3
4
5
6
7
8
9
|
- ( void )drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor); CGContextFillRect(context, rect);
CGContextSetStrokeColorWithColor(context, [UIColorcolorWithHexString:@ "ffffff" ].CGColor);
CGContextStrokeRect(context, CGRectMake(5, -1, rect.size.width - 10, 1)); //下分割线
CGContextSetStrokeColorWithColor(context, [UIColor colorWithHexString:@ "e2e2e2" ].CGColor);
CGContextStrokeRect(context, CGRectMake(5, rect.size.height, rect.size.width - 10, 1));
}
|
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对快网idc的支持。
相关文章
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-27 47
-
2025-05-27 48
-
2025-05-25 91
-
2025-05-29 85
-
浅谈C++类型转化(运算符重载函数)和基本运算符重载(自增自减)
2025-05-27 67