小码哥-解决cell点击时子视图改变背景颜色的问题

来源于:小码哥教育520it
2016-11-15 17:08:43

  前言

  iOS开发中,UITableView随处可见,而在点击UITableView的cell的时候,如果他的子视图设置了透明颜色以外的颜色,子视图的背景颜色会进行相关的改变,效果如下图。

1/10

  如果没有,我再举几个很多人使用的 App 上对于这种情况处理不佳的例子,注意左右对比

2/10
3/10

修改 cell 的选中样式:

  cell.selectionStyle =UITableViewCellSelectionStyleNone; 这种解决方案,可以正确的达到点击的时候子视图背景颜色不再改变,只是美中不足的是,这种方法不仅去除了我们不想要的子视图背景颜色改变的效果,还去除了 cell 本身 contentView 的背景颜色改变的效果。

  此时点击 cell ,用户不再会感觉到有任何变化,为了取消子视图背景颜色改变效果,而取消 cell 的选中效果,这种做法不太友好。

  此时又有网友提出,自己实现这种选中效果,在 cell 的 contentView 的最下层添加一个 button,很好的思路,只是,如果此时你的 cell 已经使用 xib 布局的差不多了,或者使用纯代码写的差不多了,再向 cell 的 contentView 和你所添加的 subview 中间添加一个 button 就显得有点麻烦。

  懒癌晚期,想有一劳永逸的解决办法,最好是那种不对原工程做任何改动的

  在这里我的思路是:拦截 cell 的 contentView 中所有子视图的设置背景颜色的方法,并在其中打印函数调用栈,看看颜色改变前后调用了哪些方法,再进行相应的变化

  cell 子视图更改颜色之前,究竟做了些什么?

  我新建了一个 .m 文件,并在其中使用 runtime 的 Method Swizzle 对设置背景颜色方法进行替换,在新的设置背景颜色方法中打印函数调用栈,具体代码如下:

4/10

  然后我们进行相关测试,分别观看点击 cell 时改变子视图背景色为透明时候的函数调用栈,以及恢复子视图背景颜色为之前颜色的函数调用栈。

5/10
6/10

  _setOpaque:forSubview: showSelectedBackgroundView:animated: setHighlighted:animated:

  等方法;知道了选中前后都调用过这三种方法之后,我们接下来只要 在其中的一个方法进行改写,然后分别 在点击前后对子视图增加不能更改背景颜色的标识,就能实现我们想要的最终效果了。

7/10
8/10
9/10
10/10

  不足之处

  这篇文章中所实现的仅仅是 UITableViewCell 在点击过程中 UILabel 的颜色不会改变,其他的子控件,诸如,UIButton,UIView 之类的视图,背景色仍然会改变,为此,我写了一个分类:CoderGin/CGCellContentViewManager

  结尾

  #import#import@interface UILabel (RuntimeTest) @property (nonatomic, assign) BOOL forbidSetBackgroundColor; @end @implementation UILabel (RuntimeTest) + (void)load { SEL originalSelector = @selector(setBackgroundColor:); SEL swizzledSelector = @selector(ex_setBackgroundColor:); Method originalMethod = class_getInstanceMethod(self, originalSelector); Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector); BOOL success = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); if (success) { class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, swizzledMethod); } } - (void)ex_setBackgroundColor:(UIColor *)backgroundColor { if (self.forbidSetBackgroundColor == NO) { [self ex_setBackgroundColor:backgroundColor]; } // NSArray *callStackSymbols = [NSThread callStackSymbols]; // if ([callStackSymbols count] 0) { // NSLog(@"-------------"); // [callStackSymbols enumerateObjectsUsingBlock:^(NSString *symbol, NSUInteger idx, BOOL * _Nonnull stop) { // printf("%s\n", symbol.UTF8String); // }]; // NSLog(@"-------------"); // } else { // NSLog(@"%@", NSStringFromSelector(_cmd)); // } } - (BOOL)forbidSetBackgroundColor { return objc_getAssociatedObject(self, _cmd); } - (void)setForbidSetBackgroundColor:(BOOL)forbidSetBackgroundColor { objc_setAssociatedObject(self, @selector(forbidSetBackgroundColor), @(forbidSetBackgroundColor), OBJC_ASSOCIATION_RETAIN_NONATOMIC); } @end @implementation UITableViewCell (RuntimeTest) + (void)load { SEL originalSelector = @selector(setHighlighted:animated:); SEL swizzledSelector = @selector(ex_setHighlighted:animated:); Method originalMethod = class_getInstanceMethod(self, originalSelector); Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector); BOOL success = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); if (success) { class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, swizzledMethod); } } - (void)ex_setHighlighted:(BOOL)highlighted animated:(BOOL)animated { if (highlighted == YES) { for (UIView *subview in self.contentView.subviews) { if ([subview isKindOfClass:[UILabel class]]) { UILabel *label = (UILabel *)subview; label.forbidSetBackgroundColor = YES; } } [self setHadSetHighLighted:YES]; } else { if ([self hadSetHighLighted]) { for (UIView *subview in self.contentView.subviews) { if ([subview isKindOfClass:[UILabel class]]) { UILabel *label = (UILabel *)subview; label.forbidSetBackgroundColor = NO; } } } } [self ex_setHighlighted:highlighted animated:animated]; } - (NSInteger)hadSetHighLighted { // 在 cell 默认被加载出来的时候,也会调用 setHighlighted:animated: 方法 return [objc_getAssociatedObject(self, _cmd) integerValue]; } - (void)setHadSetHighLighted:(BOOL)hadSetHighLighted { objc_setAssociatedObject(self, @selector(hadSetHighLighted), @(hadSetHighLighted), OBJC_ASSOCIATION_ASSIGN); } @end