【问题标题】:how to set the background color for single cell in the table view on iphone如何在iphone的表格视图中设置单个单元格的背景颜色
【发布时间】:2011-04-04 02:42:49
【问题描述】:

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

我需要你的帮助....

提前致谢.....

【问题讨论】:

    标签: iphone


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      查看这篇文章了解更多详情:Setting background color of a table view cell on iPhone

      【讨论】:

        【解决方案3】:
        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];
                }
        

        【讨论】:

          猜你喜欢
          • 2010-10-28
          • 2018-06-22
          • 2019-10-21
          • 2015-06-02
          • 1970-01-01
          • 1970-01-01
          • 2012-06-06
          • 2014-04-05
          • 1970-01-01
          相关资源
          最近更新 更多