Saturday, June 8, 2013

UITableViewCell setHighlighted animated example in Objective C (iOS).


UITableViewCell setHighlighted animated

Sets the highlighted state of the cell, optionally animating the transition between states.

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated

Parameters of [UITableViewCell setHighlighted animated]
highlighted
YES to set the cell as highlighted, NO to set it as unhighlighted. The default is NO.
animated
YES to animate the transition between highlighted states, NO to make the transition immediate.

Discussion of [UITableViewCell setHighlighted animated]
Highlights or unhighlights the cell, animating the transition between regular and highlighted state if animated is YES. Highlighting affects the appearance of the cell’s labels, image, and background.

Note that for highlighting to work properly, you must fetch the cell’s label (or labels) using the textLabel (and detailTextLabel properties and set the label’s highlightedTextColor property; for images, get the cell’s image using the imageView property and set the UIImageView object’s highlightedImage property.[UITableViewCell setHighlighted animated]

A custom table cell may override this method to make any transitory appearance changes.

UITableViewCell setHighlighted animated example.
// animate between regular and highlighted state
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated; {
    [super setHighlighted:highlighted animated:animated];

    //Set the correct background Image
    UIImageView* backgroundPicture = (UIImageView*)[self viewWithTag:HACK_BACKGROUND_VIEW_TAG];
    if (highlighted) {
        backgroundPicture.image = [UIImage imageNamed:@"FondSelected.png"];
    }
    else {
        backgroundPicture.image = [UIImage imageNamed:@"Fond.png"];
    }
}

Example of [UITableViewCell setHighlighted animated].
- (void)updateCellDisplay {
    if (self.selected || self.highlighted) {
        self.nameLabel.textColor = [UIColor lightGrayColor];
        self.colorLabel.textColor = [UIColor lightGrayColor];
    }
    else {
        self.nameLabel.textColor = [UIColor blackColor];
        self.colorLabel.textColor = [UIColor blackColor];
    }
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    [self updateCellDisplay];
}

- (void) setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    [self updateCellDisplay];
}

UITableViewCell setHighlighted animated example.
You must also override setHighlighted: to prevent the blue gradient from ever showing. If you just override setHighlighted: then you end up with a momentary selection effect.

so you'll have these two methods:

- (void)setHighlighted: (BOOL)highlighted animated: (BOOL)animated
{
    // don't highlight
}

- (void)setSelected: (BOOL)selected animated: (BOOL)animated
{
    // don't select
    //[super setSelected:selected animated:animated];
}

End of UITableViewCell setHighlighted animated example article.