Search code examples
iphoneiosobjective-ctableviewcell

How to set a color for a Cell on a TableView?


I want the whole cell printed in blue but it only shows a little band.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
[cell.contentView setBackgroundColor:[UIColor blueColor]];    
cell.textLabel.text = [NSString stringWithFormat:@"%@", [cat objectAtIndex:indexPath.row]];    
return cell;}

My screen capture is this:

enter image description here


Solution

  • add this line after [cell.contentView setBackgroundColor:[UIColor blueColor]]; line:

    cell.textLabel.backgroundColor = [UIColor clearColor];