Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable "highlight subviews" message for UIView/UIViewController in iOS SDK?

I want to use the default highlight on a UITableViewCell when it is tapped. However, I do not want custom subviews (and their subviews) to receive the message to update their highlighted states and therefore disrupt the backgroundColor property.

Edit
By "subview" I mean any UIView subclass, not just UITableViewCells.

Perhaps this hypothetical situation will better articulate what I'm looking for: I have one UITableViewCell. Call it c. I then add one UIView (call it v) as a subview of c. When I tap c, I want c to become highlighted (standard blue background with white font color), but I do not want v to become highlighted. How do I make this happen?

like image 302
Jacob Barnard Avatar asked Jun 20 '11 21:06

Jacob Barnard


2 Answers

First of all, UITableView enumarates all the subviews, and sends them highlight messages.

So even if you put a UILabel in your view, no matter how deep it is, it traverses all views (by using subviews property).

One solution can be (which is IOS4+), overriding subviews property, and cheat tableview's highlight function that we do not have any subviews. To do that we need to determine the caller, and if it is tableview's highlight method, we should return no subviews at all.

We can create a simple UIView subclass and override subviews like below.

- (NSArray *)subviews{
    NSString* backtrace = [NSString stringWithFormat: @"%@",[NSThread callStackSymbols]];
    if ([backtrace rangeOfString:@"_updateHighlightColorsForView"].location!=NSNotFound)
        return [super subviews];   

    return [[NSArray new] autorelease];
}
  • callStackSymbols is available after IOS4+
  • _updateHighlightColorsForView is the UITableView's method, responsible for highlighting all children
like image 191
Deniz Mert Edincik Avatar answered Oct 01 '22 19:10

Deniz Mert Edincik


I have a class which inherits from UITableViewCell, so i fixed it overriding setSelected:animated like this:

   - (void)setSelected:(BOOL)selected animated:(BOOL)animated
   {
       [super setSelected:selected animated:animated]; 
       //Reset highlighted status to all the childs that i care for.
       [self.someChild setHighlighted:NO];
       [self.someOtherChild setHighlighted:NO];
   }
like image 36
albertein Avatar answered Oct 01 '22 18:10

albertein