最近项目中用到UITableView中cell之间要用自定义的虚线分割,然后选中cell出现淡黄色效果。按照逻辑出现自定义效果可以自己创建一个继承UITableViewCell的类,里面加上:UIView *lineView = [[UIView alloc] init];
lineView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"CR_meetingroomlist_Line"]];
lineView.frame = CGRectMake(17, self.frame.size.height - 1,402,1);
[self addSubview:lineView];
然后在用到cell的这个方法里- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath加上 cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:250.0f/255.0f green:250.0f/255.0f blue:198.0f/255.0f alpha:1];就可以点击cell出现淡黄色效果。此时的问题出现了,其实cell.selectedBackgroundView的大小你是无法去修改的,意思是即使你修改了它还是cell的大小。所以当你选中cell时,背景色老是把你的虚线给覆盖住。如果你设置点击后就消失的话,就仿佛那条分割线在跳动的感觉,让人很不舒服。之前一直以为是cell.selectedBackgroundView不对,就一直试图修改它的大小,后来发现这是个错误。我们正确的做法很简单:meetingRoomTBView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"CR_meetingroomlist_Line"]];在UITableView上下功夫,而不是在UITableViewCell做改变。这样的话就不会出现背景色覆盖分割线的问题了。
转载于:https://my.oschina.net/u/584517/blog/226254