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. win7电脑共享VPN连接教程

    互通网络VPN服务器不限制连接数,如果仅仅是电脑连接的话有点浪费,如何只在笔记本电脑上设置一次VPN,然后手机.平板等都可以直接共享使用呢?为什么需要笔记本电脑,因为笔记本电脑内置的无线网卡可以设置w ...

  2. 常用Sql语句书写

    一.增:有2种方法 1.使用insert插入单行数据: 语法:insert [into] <表名> [列名] values <列值> 例:insert into Strdent ...

  3. LeetCode OJ-- String to Integer (atoi) **

    https://oj.leetcode.com/problems/string-to-integer-atoi/ 细节题,把一个字符串转换成整数 class Solution { public: in ...

  4. linux(ubuntu)安装时遇到的问题

    window环境下安装linux虚拟机=时,由于在初始系统语言选择了中文,当linux虚拟机安装成功后, 按[Ctrl + alt +f1~f6]任一一键都行,进入到命令行模式,这时你会发现,哎,我的 ...

  5. C语言误区

    int date[10] ; //定义一个字长10的数组,从date[0]....date[9] static   char   c ;   //静态变量(有静态外部变量和静态局部变量),静态外部变量 ...

  6. (并查集)~APTX4869(fzu 2233)

    http://acm.fzu.edu.cn/problem.php?pid=2233 Problem Description 为了帮助柯南回到一米七四,阿笠博士夜以继日地研究APTX4869的解药.他 ...

  7. Eclipse 创建Maven项目的问题:a pom xml file already exists in the destination folder

    创建过一个Maven项目,删除的时候只在Eclipse中删除了,但是磁盘上的这个项目没有删除,所以报错 方法:重新创建一个不同名称的Maven项目,右键项目,选择Properties,看你的项目目录( ...

  8. 【基础知识】.Net基础加强06天

    一. 垃圾回收 1. 垃圾回收的目的:提高内存的利用效率. 2. 垃圾回收器: 只回收托管堆中的内存资源,不回收其他资源(数据库连接.文件句柄.网络端口等): 3. 什么时候垃圾回收? a) 当对象没 ...

  9. mongodb(二) 安装和使用

    mongodb的安装和使用 最近的项目需要使用到mongodb,从而开始熟悉nosql,有了本篇文章,记录和方便他人. mongodb的安装 下载地址:http://www.mongodb.org/d ...

  10. [OpenGL] 1、环境搭建及最小系统

    >_<: 首先推荐一个企业版的VC6.0自带OpenGL和DirectX,非常方便:http://pan.baidu.com/s/1mgIAGi8 PS: 要注意这里的OpenGL建立的工 ...