Home Tutorial Iphone Examples UITableViewCell Color

 
 

UITableViewCell Color
Posted on: May 5, 2010 at 12:00 AM
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

Related Tags for UITableViewCell Color:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.