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上得问题。

最新文章

  1. RequireJS基础(二)
  2. 介绍开源的.net通信框架NetworkComms框架 源码分析(二十二 )TCPConnectionStatic
  3. NoSQL学习二:MongoDB基本管理命令
  4. jQuery事件流的顺序
  5. cf 710E dp
  6. C语言基础--for循环
  7. Android应用之《宋词三百首》(二)
  8. jquery如何在加载完iframe的内容后才进行下一步操作
  9. java中Executor、ExecutorService、ThreadPoolExecutor介绍(转)
  10. python基础(三)--列表、元组、字典
  11. 转:Redis 配置为 Service 系统服务
  12. 从《海贼王》的视角走进BAT的世界(百度/阿里/腾讯)
  13. AspNetCore Mvc 使用 PartialView
  14. B - Housewife Wind-树链剖分-树状数组
  15. Git submodule 仓库中包含另外的仓库(子仓库)
  16. webstorm 智能提示忽略大小写
  17. iOS - 常用本机URL跳转设置
  18. 20135202闫佳歆--week6 分析Linux内核创建一个新进程的过程——实验及总结
  19. Oracle时间的加减
  20. linux体系结构与内核结构图解

热门文章

  1. 精妙SQL语句收集
  2. UWP深入学习五: 传感器与搜索、共享及链接
  3. C++矢量图形库系列(1)——矢量图形库乱谈(转)
  4. Android 如何制造低内存环境
  5. 黑马程序员_Java基础:实现多线程对共有数据的同步操作
  6. [数据结构] N皇后问题
  7. 内存不足 java.lang.OutOfMemoryError: PermGen space
  8. 老调重弹:JDBC系列之<驱动加载原理全面解析) ----转
  9. MyBatis学习之路之configuration配置
  10. Linux 配置主机名