Saturday, June 8, 2013

UITableViewCell setSelected example in Objective C (iOS).

UITableViewCell setSelected

A Boolean value that indicates whether the cell is selected.

@property(nonatomic, getter=isSelected) BOOL selected

Discussion of [UITableViewCell setSelected]
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 setSelected 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].
Try to type:

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

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

End of UITableViewCell setSelected example article.