UITableView 選中cell ,默認會有一個灰色的背景遮罩效果,這個灰色遮罩就是cell 自帶的ios
selectedBackgroundViewapp
咱們能夠設置selectedBackgroundView 的frame 、和 背景顏色ui
selectedBackgroundView.backgroundColoratom
- (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 ,沒有彈起,遮罩顯示時)。spa
這™明顯bug ,在ios7以後。code
這個裏面的回答都試過遍了仍是不行。blog
本身搞吧,決定不用系統的分割線,本身加一個試試看。ip
在你cell 的基類中或 自定義cell 中添加這兩個方法get
#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上得問題。