Share on Google+Share on Google+

UITableViewCell Color

Advertisement
By default UITableViewCell takes "StyleNone" except this default style it has two more style called StyleBlue and StyleGray.

UITableViewCell Color

In this example, we going to learn how to set the background color on cell in UITableView.

By default UITableViewCell takes "StyleNone" except this default style it has two more style called StyleBlue and StyleGray. But apart from it we also have some option to change the cell background color on selection. In this example we are going to change the default cell color to orange color that will be shown on selection.

Syntax - Change UITableViewCell Color

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

//To Set Selected Background Color
UIImageView *selectedBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320,100)];
selectedBackground.backgroundColor = [UIColor orangeColor];
[cell setSelectedBackgroundView:selectedBackground];
}
// Configure the cell.
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

return cell;
}

As you can see into the foregoing code we are creating a UIImageView with particular frame size and assigning a color to it, which will be added as subview directly above the backgroundView. You can see the effect on selected cell of UITableView.

On building the application you should get the following output.

Download Code

Advertisements

Posted on: May 5, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.