如何在iphone的表格视图中设置单个单元格的背景颜色

时间:2010-08-26 06:47:39

标签: iphone

我有一个表视图。因为我想单独设置第一个单元格的颜色。其他颜色应该是白色......我怎么能这样做

我需要你的帮助......

提前致谢.....

3 个答案:

答案 0 :(得分:2)

在cellForRowAtIndexPath中,检查indexPath.row == 0,然后设置自定义背景。否则设置默认背景。

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

答案 1 :(得分:0)

您应该在“willDisplayCell”委托方法中进行颜色更改,而不是在“cellForRowAtIndexPath”中创建颜色。 否则,在向电池添加附件时,它可能不会粘住或显示不良。

请查看此帖子了解更多详情:Setting background color of a table view cell on iPhone

答案 2 :(得分: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];
        }