Susee
Susee

Reputation: 13

how to set the background color for single cell in the table view on iphone

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.....

Upvotes: 0

Views: 584

Answers (3)

Bhavesh Nayi
Bhavesh Nayi

Reputation: 3656

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];
        }

Upvotes: 0

Oliver
Oliver

Reputation: 23510

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

Upvotes: 0

mclin
mclin

Reputation: 3629

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;
}

Upvotes: 2

Related Questions