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之后。

http://stackoverflow.com/questions/19212476/uitableview-separator-line-disappears-when-selecting-cells-in-ios7

这个里面的回答都试过遍了还是不行。

自己搞吧,决定不用系统的分割线,自己加一个试试看。

在你cell 的基类中或 自定义cell 中添加这两个方法


#define  separatorViewTag  10456


@interface MyCustomTableViewCell(){
UIView *customSeparatorView;
CGFloat separatorHight;
}
@property (nonatomic,weak)UIView *originSeparatorView;
@end

/**

*  设置分割线 , separatorInset 不一定能实现

*  解决选中cell selectedBackgroundView 显示时 分割线显示不全

*  问题参见:http://stackoverflow.com/questions/19212476/uitableview-separator-line-disappears-when-selecting-cells-in-ios7

*  @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 选中时有些不显示的更多相关文章

  1. iOS UITableView 移除单元格选中时的高亮状态

    在处理UITableView表格时,我们希望用户能够和触摸单元格式进行交互,但是希望用户在完成交互之后,这些单元格的选中状态能够消失,.Cocoa Touch 提供了两种方法来防止单元格背持久选中. ...

  2. 1016-06-首页20-封装工具条---UITableView控件距离顶部的间距问题----cell选中时的背景颜色设置

    一.设置UITableView里面的顶部 cell 距离顶部的间距的三种方式: 方法 1. 直接设置: self.tableView.contentInset = UIEdgeInsetsMake(H ...

  3. IOS - UITableViewCell的选中时的颜色及tableViewCell的selecte与deselecte

    1.系统默认的颜色设置 [cpp] view plaincopy //无色 cell.selectionStyle = UITableViewCellSelectionStyleNone; //蓝色 ...

  4. 【转】iOS开发UITableViewCell的选中时的颜色设置

    原文网址:http://mobile.51cto.com/hot-404900.htm 1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSe ...

  5. UITableViewCell的选中时的颜色及tableViewCell的selecte与deselecte

    1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSelectionStyleNone; //蓝色 cell.selectionStyle = ...

  6. iOS开发UITableViewCell的选中时的颜色设置(转)

    iOS开发UITableViewCell的选中时的颜色设置   1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSelectionStyle ...

  7. UITableViewCell的选中时的颜色设置

    转自:http://hi.baidu.com/zhu410289616/item/0de0262910886011097508c2 1.系统默认的颜色设置 //无色 cell.selectionSty ...

  8. iOS开发UITableViewCell的选中时的颜色设置

    1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSelectionStyleNone; //蓝色 cell.selectionStyle = ...

  9. IOS中设置cell的背景view和选中时的背景view 、设置cell最右边的指示器(比如箭头\文本标签)

    一.Cell的设置 1.设置cell的背景view和选中时的背景view UIImageView *bg = [[UIImageView alloc] init]; bg.image = [UIIma ...

随机推荐

  1. Java里的构造函数(构造方法)

    构造函数 ,是一种特殊的方法.主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中.特别的一个类可以有多个构造函数 ,可根据其参数个数的不同或参数类型 ...

  2. 使用的组件:ckeditor

    老牌Web文本编辑器,无需多言. 官网地址:http://ckeditor.com/

  3. 【原创】“借贷宝”砸钱,邀请码 GZZKZK2 (注册成功每人可得20现金,可直接提现)。。。而这只是开始

    作为IT/互联网资深码农的我,从专业技术角度剖析其流程,确认其各个环节控制严格,无欺诈嫌疑, 最佳运气邀请码 : GZZKZK2, 你在注册时值得拥有, 无邀请码无奖励, 亲一定要记住.对 APP操作 ...

  4. Asp.Net Web API 2第八课——Web API 2中的属性路由

    前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 路由就是Web API如何 ...

  5. Chrome以https访问gitlab的问题:Your connection is not private

    在Chrome中以https访问自己搭建的gitlab站点时经常出现下面的错误: Attackers might be trying to steal your information from xx ...

  6. google hack 之 查询语法

    google hack 之 查询语法 文/玄魂   前言 谷歌网页搜索技术,大部分在百度等搜索引擎中也适用.同样,这些搜索技术是来源于传统数据库检索技术,因而,对这部分的学习,能为后续章节的数据库检索 ...

  7. Html5 学习系列(六)Html5本地存储和本地数据库

    一个网站如何能在客户的浏览器存储更多的数据呢? 在Html4的时代在浏览器端存储点网站个性化的数据,尤其是用户浏览器的痕迹,用户的相关数据等一般只能存储在Cookie中,但是大多是浏览器对于Cooki ...

  8. 关于网卡eth0、eth1以及服务器为什么要把内网和外网卡区分开

    在搜搜上看到了这个回答,它解释了什么是eth0,eth1: eth0和eth1这是网卡设备,只是个名称不必纠结.通常服务器会有多个网卡的,所以就有eth0 eth1 eth2 这样的名称,而且在一些系 ...

  9. paip.编程语言方法重载实现的原理及python,php,js中实现方法重载

    paip.编程语言方法重载实现的原理及python,php,js中实现方法重载 有些语言,在方法的重载上,形式上不支持函数重载,但可以通过模拟实现.. 主要原理:根据参数个数进行重载,或者使用默认值 ...

  10. paip.提升稳定性---c3p0数据库连接池不能取到连接An attempt by a client to checkout a Connection has timed out

    paip.提升稳定性---c3p0数据库连接池不能取到连接An attempt by a client to checkout a Connection has timed out 作者Attilax ...