0

I'm having one Table view. In that i want to set the color for the First cell alone..Others should be in white...How can i do that

I need your help....

Thanks in advance.....

Susee
  • 13
  • 3

3 Answers3

2

In cellForRowAtIndexPath, check if the indexPath.row == 0, then set the custom background. Otherwise set the default background.

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    if (indexPath.row == 0) {
        //set custom background color
    } else {
        //set default background color
    }
    return cell;
}
mclin
  • 3,599
  • 1
  • 20
  • 18
0
if(indexPath.row == 0)
        {
            UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
            bg.backgroundColor = [UIColor colorWithRed:175.0/255.0 green:220.0/255.0 blue:186.0/255.0 alpha:1]; 
            cell.backgroundView = bg;
            [bg release];
        }
Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42
0

You should do the color change in the "willDisplayCell" delegate method and not at its creation in "cellForRowAtIndexPath". Otherwise it may not stick or may display bad when adding, for example, accessories to the cell.

Look at this post for more details : Setting background color of a table view cell on iPhone

Community
  • 1
  • 1
Oliver
  • 23,072
  • 33
  • 138
  • 230