ios7 UITableView 分割线在 使用selectedBackgroundView 选中时有些不显示
UITableView 选中cell ,默认会有一个灰色的背景遮罩效果,这个灰色遮罩就是cell 自带的
selectedBackgroundView
我们可以设置selectedBackgroundView 的frame 、和 背景颜色
selectedBackgroundView.backgroundColor
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { cell.selectedBackgroundView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView.frame = cell.frame; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self performSelector:@selector(unselectCell:) withObject:nil afterDelay:0.2]; } -(void)unselectCell:(id)sender{ [_setUpTableView deselectRowAtIndexPath:[_setUpTableView indexPathForSelectedRow] animated:YES];
}
这样就可以自定义选中效果啦。问题来拉,当选中一个cell时,你会发现这个cell 想临的cell 的分割线没啦(按下cell ,没有弹起,遮罩显示时)。
这™明显bug ,在ios7之后。
这个里面的回答都试过遍了还是不行。
自己搞吧,决定不用系统的分割线,自己加一个试试看。
在你cell 的基类中或 自定义cell 中添加这两个方法
#define separatorViewTag 10456
@interface MyCustomTableViewCell(){
UIView *customSeparatorView;
CGFloat separatorHight;
}
@property (nonatomic,weak)UIView *originSeparatorView;
@end
/**
* 设置分割线 , separatorInset 不一定能实现
* 解决选中cell selectedBackgroundView 显示时 分割线显示不全
* @param insets insets description
*/
-(void)setSeparatorWithInset:(UIEdgeInsets)insets{
if (customSeparatorView) {
customSeparatorView.frame = CGRectMake(insets.left, insets.top,self.width - insets.left - insets.right, self.originSeparatorView.height-insets.bottom - insets.top);
self.originSeparatorView.hidden = YES;
self.originSeparatorView.alpha = 0;
}else{
for (int i = ([self.contentView.superview.subviews count] - 1); i >= 0; i--) {
UIView *subView = self.contentView.superview.subviews[i];
if ([NSStringFromClass(subView.class) hasSuffix:@"SeparatorView"]) {
self.originSeparatorView = subView;
subView.hidden = YES;
subView.alpha = 0;
subView.frame = CGRectMake(insets.left, insets.top,self.width - insets.left - insets.right, subView.height-insets.bottom - insets.top);
customSeparatorView = [[subView superview] viewWithTag:separatorViewTag];
if (!customSeparatorView) {
customSeparatorView = [[UIView alloc] initWithFrame:subView.frame];
customSeparatorView.tag = separatorViewTag;
[[subView superview] addSubview:customSeparatorView];
customSeparatorView.backgroundColor = [subView backgroundColor];
}
[[subView superview] bringSubviewToFront:customSeparatorView];
break;
}
}
}
}
-(void)setSeparatorColorWithColor:(UIColor *)sepColor{
if (customSeparatorView) {
customSeparatorView.backgroundColor = sepColor;
self.originSeparatorView.hidden = YES;
self.originSeparatorView.alpha = 0;
}else {
for (int i = ([self.contentView.superview.subviews count] - 1); i >= 0; i--) {
UIView *subView = self.contentView.superview.subviews[i];
if ([NSStringFromClass(subView.class) hasSuffix:@"SeparatorView"]) {
self.originSeparatorView = subView;
if (sepColor) {
subView.hidden = YES;
subView.alpha = 0;
subView.backgroundColor = sepColor;
customSeparatorView = [[subView superview] viewWithTag:separatorViewTag];
if (!customSeparatorView) {
customSeparatorView = [[UIView alloc] initWithFrame:subView.frame];
customSeparatorView.tag = separatorViewTag;
[[subView superview] addSubview:customSeparatorView];
customSeparatorView.backgroundColor = [subView backgroundColor];
}
[[subView superview] bringSubviewToFront:customSeparatorView];
}
break;
}
}
}
}
-(void)layoutSubviews{
[super layoutSubviews];
[self setSeparatorWithInset:UIEdgeInsetsMake(0, 0, 0, 0)];
[self setSeparatorColorWithColor:[UIColor colorWithRed:31/255.0 green:32/255.0f blue:35/255.0 alpha:0.2]];
}
一个是设置分割线frame 的 一个是设置颜色。
其中遍历cell的subview 倒序。(提高效率)找到分割线,隐藏掉,重写一个view 加在分割线的superview 上。这样上面问题中使用selectedBackgroundView选中cell 时影响分割线的问题就解决啦。
注:tableview 使用section展示好像没事(把cell 都放到section 里),我项目设置页面就是这样没有问题使用selectedBackgroundView。当只有一个section时会出现上述问题。
最后我去回答下stackoverflow上得问题。
ios7 UITableView 分割线在 使用selectedBackgroundView 选中时有些不显示的更多相关文章
- iOS UITableView 移除单元格选中时的高亮状态
在处理UITableView表格时,我们希望用户能够和触摸单元格式进行交互,但是希望用户在完成交互之后,这些单元格的选中状态能够消失,.Cocoa Touch 提供了两种方法来防止单元格背持久选中. ...
- 1016-06-首页20-封装工具条---UITableView控件距离顶部的间距问题----cell选中时的背景颜色设置
一.设置UITableView里面的顶部 cell 距离顶部的间距的三种方式: 方法 1. 直接设置: self.tableView.contentInset = UIEdgeInsetsMake(H ...
- IOS - UITableViewCell的选中时的颜色及tableViewCell的selecte与deselecte
1.系统默认的颜色设置 [cpp] view plaincopy //无色 cell.selectionStyle = UITableViewCellSelectionStyleNone; //蓝色 ...
- 【转】iOS开发UITableViewCell的选中时的颜色设置
原文网址:http://mobile.51cto.com/hot-404900.htm 1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSe ...
- UITableViewCell的选中时的颜色及tableViewCell的selecte与deselecte
1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSelectionStyleNone; //蓝色 cell.selectionStyle = ...
- iOS开发UITableViewCell的选中时的颜色设置(转)
iOS开发UITableViewCell的选中时的颜色设置 1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSelectionStyle ...
- UITableViewCell的选中时的颜色设置
转自:http://hi.baidu.com/zhu410289616/item/0de0262910886011097508c2 1.系统默认的颜色设置 //无色 cell.selectionSty ...
- iOS开发UITableViewCell的选中时的颜色设置
1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSelectionStyleNone; //蓝色 cell.selectionStyle = ...
- IOS中设置cell的背景view和选中时的背景view 、设置cell最右边的指示器(比如箭头\文本标签)
一.Cell的设置 1.设置cell的背景view和选中时的背景view UIImageView *bg = [[UIImageView alloc] init]; bg.image = [UIIma ...
随机推荐
- JS无缝滚动
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 31.0px Consolas; color: #2b7ec3 } p.p2 { margin: 0.0px ...
- Create My MySQL configuration by Percona
本文地址:http://www.cnblogs.com/yhLinux/p/4013065.html https://tools.percona.com/ Percona是一款在线自动生成MySQL配 ...
- FreeMarker与Spring MVC的结合应用
Freemarker是一种基于java的模板引擎.SpringMVC对FreeMarker进行一些配置的支持,能够利用Freemarker只关注表现层以及Spring MVC的三层分离的特点,向前端输 ...
- Python模拟C++输出流
看到一Python例子,挺有意思的,用Python模拟C++的输出流OStream.单纯只是玩. 原理: 利用Python __lshift__左移内建函数<<,调用时将输出内容,如果内容 ...
- python import 模块异常问题
新项目开始了,(tornado,python) 结果出现了一个比较诡异的问题. import 自己的在conf文件夹下配置文件settings的时候, from conf.settings impor ...
- International Conference in 2014
International Conference on Machine Learning (ICML2014, Beijing).(papers are available) Neural Infor ...
- haskell中的let和where
haskell中有两种定义局部变量的方法let和where,方法分别如下 roots a b c = ((-b + det) / (a2), (-b - det) / (a2)) *a*c) a2 = ...
- [转]解决WebClient或HttpWebRequest首次连接缓慢问题
http://blog.csdn.net/rrrfff/article/details/6170653?reload 设置代理为空: <?xml version="1.0"? ...
- 利用Junit4进行程序模块的测试,回归测试
①在你的工程里导入JUnit4的包 ②右击创建JUnit测试类,在测试类中编写测试代码即可. JUnit 目前需要掌握的有一下几点: Fixture系列:BeforeClass,AfterClass, ...
- Asp.Net MVC Filter 实现方式和作用范围控制
MVC中的Filte 简单又优雅的实现了AOP ,在日志,权限,缓存和异常处理等方面用的比较多.但本文不是讨论Filter这些功能点,而是总结Filter实现的方式.说实现也不太准确,也就是它的呈现方 ...