首页 文章

UITableViewCell显示白色背景,无法在iOS7上修改

提问于
浏览
184

我已经实现了一个继承自 UITableViewCell 的自定义表视图单元类 . tableview包含一个背景图像,所以我希望单元格的背景是透明的 . 它在iOS7之前看起来很棒 .

但是,在iOS7中,单元格始终显示为白色背景 .


即使对于Xcode7,2015,也有 is a bug in storyboard :您必须在代码中设置单元格的背景颜色 .

14 回答

  • 2

    正如Apple DOC所说(UITableViewCell Class Reference):

    ...在iOS 7中,单元格默认为白色背景;在早期版本的iOS中,单元格继承封闭表视图的背景颜色 . 如果要更改单元格的背景颜色,请在表视图委托的tableView:willDisplayCell:forRowAtIndexPath:方法中执行此操作 .

    因此,对于我的情况,要显示具有透明背景的单元格,只需要在表视图控制器中实现委托方法,如下所示:

    - (void)tableView:(UITableView *)tableView
      willDisplayCell:(UITableViewCell *)cell
    forRowAtIndexPath:(NSIndexPath *)indexPath
    {
      [cell setBackgroundColor:[UIColor clearColor]];
    }
    

    Just Note :正如@null所说,我并不完全确定它是否确实存在这个错误,但似乎是因为他的评论得到了几票 . 如果你使用IB,可能会出现问题 . :)

  • 3

    我调查了一下,发现单元backgroundColor是由Appearance系统设置的 . 因此,如果应用程序中的所有单元格都具有明确的背景,则最简单的解决方案是:

    [[UITableViewCell appearance] setBackgroundColor:[UIColor clearColor]];
    

    即使它们具有不同的背景,清晰的颜色似乎是最方便的默认颜色 .

  • 1

    在返回单元格之前将其写入cellForRowAtIndexPath方法;

    cell.backgroundColor = [UIColor clearColor];
    
  • 13

    iOS 7中 UITableViewCell 的默认背景颜色为白色 .

    您必须在代码中的某处设置 backgroundColor 属性 . 例如,在新创建单元格后设置它 .

    cell.backgroundColor = [UIColor clearColor];
    
  • 2

    我实际上发现问题是由于UITableView的背景颜色未设置为清除 . 如果您将UITableViewCell的背景颜色更改为清除并且您发现仍然看到白色,请确保将UITableView的背景颜色设置为清除(或任何您想要的颜色) .

    [self.tableView setBackgroundView:nil];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    

    In Swift

    tableView.backgroundView = nil
    tableView.backgroundColor = UIColor.clearColor()
    
  • 28

    这绝对是一个iOS错误 . 在iOS 8中,在_1545803中设置背景颜色适用于iPhone,但在iPad中它总是 whiteColor . 要修复它,请在 cellForIndexPath 数据源消息中,将其放入:

    cell.backgroundColor = cell.backgroundColor

    它会奏效 . 这正是我说这是一个错误的原因,因为代码本身几乎是蹩脚的,但它确实有效 .

  • 0

    可能是您的UITableViewCell默认选中 . 您可以使用Xcode 6s new visual debugger确认这一点(或确切地找出导致此白色单元出现的视图) .

    enter image description here

    有趣的是,在知道这个..设置所选单元格的背景颜色以清除后仍然没有创建这个自定义单元格:

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            // do customization here
        }
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setBackgroundColor:[UIColor clearColor]];
        return self;
    }
    
  • 65

    我发现上述所有内容都不适用于XCode 5.1.1,iOS 7.1 .

    如果将Interface Builder与原型单元格一起使用,请选择原型单元格,然后从“视图”部分的属性选择器中,将“背景”窗体的默认值更改为“清除颜色”:

    enter image description here

    这似乎工作正常 . 也不需要上述代码更改 - 这是纯粹的IB解决方案 .

  • 13

    接受的答案并没有解决我的问题 . 我不得不这样做:

    • 在界面构建器中,选择表视图 . 然后从属性检查器的 View 部分中,将 Background 设置为透明(0%不透明度) .

    enter image description here

    • 从表的数据源类cellForRowAtIndexPath方法:
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
        reuseIdentifier:CellIdentifier];
    cell.backgroundColor = [UIColor clearColor]; //Make cell transparent
    
  • 4

    对我来说,在 tableView:willDisplayCell:forRowAtIndexPath:tableView:cell:forRowAtIndexPath: 设置 cell.backgroundColor = cell.contentView.backgroundColor; 完成了这项工作 .

    这是因为我根据需要在Interface Builder中设置了contentView的背景颜色 .

  • 10

    将UI对象添加到单元格时,Xcode 5 / IOS 7添加了一个新的“内容视图”,它将成为单元格中所有元素的超级视图 . 要设置单元格的背景颜色,请设置此内容视图的背景颜色 . 上面的解决方案对我没有用,但这个对我来说效果很好 .

  • 383

    Swift 1.2解决方案:

    只需为您的单元格的背景颜色添加一个明确的定义,如下所示:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        // Configure the cell...
        cell.backgroundColor = UIColor.clearColor()
    
        return cell
    }
    
  • 1

    有类似的问题,不得不

    • 将blue设置为selectionStyle和

    • 将此内容添加到代码中以进行更正

    override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    
      var bgColorView: UIView = UIView()
      bgColorView.backgroundColor = UIColor(red: (76.0 / 255.0), green: (161.0 / 255.0), blue: (255.0 / 255.0), alpha: 1.0)
      bgColorView.layer.masksToBounds = true
      tableView.cellForRowAtIndexPath(indexPath)!.selectedBackgroundView = bgColorView
      return true
    }
    
  • 1

    您还可以使用颜色代码,以使您的UITableView Cell获利丰厚 .

    [cell setBackgroundColor:[self colorWithHexString:@"1fbbff"]];
    

    这是应用颜色代码方法的代码:

    -(UIColor*)colorWithHexString:(NSString*)hex
    {
        NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    
        if ([cString length] < 6) return [UIColor grayColor];
    
        if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    
        if ([cString length] != 6) return  [UIColor grayColor];
    
        NSRange range;
        range.location = 0;
        range.length = 2;
        NSString *rString = [cString substringWithRange:range];
    
        range.location = 2;
        NSString *gString = [cString substringWithRange:range];
    
        range.location = 4;
        NSString *bString = [cString substringWithRange:range];
    
        unsigned int r, g, b;
        [[NSScanner scannerWithString:rString] scanHexInt:&r];
        [[NSScanner scannerWithString:gString] scanHexInt:&g];
        [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
        return [UIColor colorWithRed:((float) r / 255.0f)
                             green:((float) g / 255.0f)
                              blue:((float) b / 255.0f)
                             alpha:1.0f];
    }
    

相关问题