Saturday, June 8, 2013

UITableViewCell setSelected animated example in Objective C (iOS).


UITableViewCell setSelected animated

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

- (void)setSelected:(BOOL)selected animated:(BOOL)animated

Parameters[UITableViewCell setSelected animated]
selected
YES to set the cell as selected, NO to set it as unselected. The default is NO.
animated
YES to animate the transition between selected states, NO to make the transition immediate.

Discussion of [UITableViewCell setSelected animated]
The selection affects the appearance of labels, image, and background. When the selected state of a cell is YES, it draws the background for selected cells (“Reusing Cells”) with its title in white.

UITableViewCell setSelected 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];
}

Example of [UITableViewCell setSelected animated].
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    UIImage *selectionBackground = [UIImage imageNamed:@"yellow_bar.png"];
    UIImageView *iview=[[UIImageView alloc] initWithImage:selectionBackground];
    self.selectedBackgroundView=iview;
}

UITableViewCell setSelected animated example.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    UIView *view = [[UIView alloc] initWithFrame:self.frame];
    view.backgroundColor = [UIColor colorWithRed:.9 green:.0 blue:.125 alpha:1.0];

    self.selectedBackgroundView = view;

    [super setSelected:selected animated:animated];
}

End of UITableViewCell setSelected animated example article.