Saturday, June 8, 2013

UITableViewCell selected example in Objective C (iOS).


UITableViewCell selected

A Boolean value that indicates whether the cell is selected.

@property(nonatomic, getter=isSelected) BOOL selected

Discussion of [UITableViewCell selected]
The selection affects the appearance of labels, image, and background. When the selected state of a cell is set to YES, it draws the background for selected cells with its title in white. The default value is NO. If you set the selection state to YES through this property, the transition to the new state appearance is not animated. For animated selected-state transitions, see the setSelected:animated: method.

UITableViewCell selected 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 selected].
Try to type:

cell.selected = NO;
It will deselect your row when needed.

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

End of UITableViewCell selected example article.