Friday, June 7, 2013

UITableViewCell setBackgroundView example in Objective C (iOS).


UITableViewCell setBackgroundView

The view used as the background of the cell.

@property(nonatomic, retain) UIView *backgroundView

Discussion of [UITableViewCell setBackgroundView]
The default is nil for cells in plain-style tables (UITableViewStylePlain) and non-nil for grouped-style tables UITableViewStyleGrouped). UITableViewCell adds the background view as a subview behind all other views and uses its current frame location.

UITableViewCell setBackgroundView example.
For Cell

    cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell_normal.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease]; 
    cell.selectedBackgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell_pressed.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
For Tableview

    [mEditTableView setBackgroundView:nil];
    [mEditTableView setBackgroundView:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"apple.png"]] autorelease]];

Example of [UITableViewCell setBackgroundView].
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DateCellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:DateCellIdentifier] autorelease]
            UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"gradient.png"]];
            [cell setBackgroundView:img];
            [img release];
        }
        cell.textLabel.text = @"Text";
}

UITableViewCell setBackgroundView example.
 if(!cell) {
cell = [[[UITableViewCell alloc] initWithFrame: ...
UIImageView *cellBG = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellbg.jpg"]];
[cell setBackgroundView:cellBG];
[cellBG release];
}

End of UITableViewCell setBackgroundView example article.