Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
UITableViewCell显示白色背景,无法在iOS7上修改_Ios_Objective C_Cocoa Touch_Ios7 - Fatal编程技术网

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

UITableViewCell显示白色背景,无法在iOS7上修改,ios,objective-c,cocoa-touch,ios7,Ios,Objective C,Cocoa Touch,Ios7,我实现了一个自定义表视图单元格类,该类继承自UITableViewCell。tableview包含一个背景图像,所以我希望单元格的背景是透明的。在7号之前看起来很棒 但是,在iOS7中,单元格始终显示为白色背景 即使对于Xcode7,2015,故事板中也有一个bug:你必须在代码中设置单元格的背景色 正如苹果医生所说(): 在iOS 7中,单元格默认为白色背景;在iOS的早期版本中,单元格继承封闭表视图的背景色。如果要更改单元格的背景色,请在表视图委托的tableView:willDispla

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

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


即使对于Xcode7,2015,故事板中也有一个bug:你必须在代码中设置单元格的背景色

正如苹果医生所说():

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

因此,在我的示例中,要显示背景透明的单元格,只需在table view控制器中实现委托方法,如下所示:

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


请注意:正如@null所说,“…界面生成器中似乎有一个bug…”,我不完全确定它是否有bug,但似乎有,因为他的评论获得了好几票。因此,如果您使用IB.:)

在iOS 7中,
UITableViewCell
的默认背景色是白色

您必须在代码中的某个位置设置
backgroundColor
属性。例如,在新创建单元后进行设置

cell.backgroundColor = [UIColor clearColor];

向单元格添加UI对象时,Xcode 5/IOS 7会添加一个新的“内容视图”,它将是单元格中所有元素的超级视图。要设置单元格的背景色,请设置此内容视图的背景色。上面的解决方案对我不起作用,但这一个对我很有效。

在返回单元格之前,在您的cellForRowAtIndexPath方法中写下这一点

cell.backgroundColor = [UIColor clearColor];

被接受的答案并没有解决我的问题。我必须这样做:

  • 在界面生成器中,选择表格视图。然后从属性检查器的视图部分,将背景设置为透明(0%不透明度)
  • 从表的数据源类cellForRowAtIndexPath方法:

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
        reuseIdentifier:CellIdentifier];
    cell.backgroundColor = [UIColor clearColor]; //Make cell transparent
    

  • 您还可以使用颜色代码,以使UITableView单元格利润丰厚

    [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];
    }
    
    -(UIColor*)colorWithHexString:(NSString*)hex
    {
    NSString*cString=[[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]大写字符串];
    if([cString length]<6)返回[UIColor grayColor];
    如果([cString hasPrefix:@“0X]”)cString=[cString substringFromIndex:2];
    如果([cString length]!=6)返回[UIColor grayColor];
    NSRange范围;
    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];
    无符号整数r,g,b;
    [[NSScanner scannerWithString:rsString]scanHexInt:&r];
    [[NSScanner scannerWithString:gString]scanHexInt:&g];
    [[NSScanner SCANNER WITHSTRING:bString]scanHexInt:&b];
    返回[UIColor COLOR WITHRED:((浮动)r/255.0f)
    绿色:((浮动)g/255.0f)
    蓝色:((浮动)b/255.0f)
    α:1.0f];
    }
    
    我发现问题是由于UITableView的背景色未设置为“清除”。如果将UITableViewCell的背景色更改为“清除”,但发现仍显示白色,请确保将UITableView的背景色设置为“清除”(或任何您想要的颜色)

    在Swift中

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

    我调查了一下,发现细胞背景颜色是由外观系统设置的。因此,如果应用程序中的所有单元格都有清晰的背景,最简单的解决方案是:

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

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

    我发现上面的任何一个都不能在XCode 5.1.1和iOS 7.1中使用

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


    这似乎很有效。也不需要对上述代码进行任何更改——这是一个纯IB解决方案。

    可能是默认情况下选择了UITableViewCell。。您可以使用Xcode 6s new确认这一点(或者确切地找出导致此白色单元格出现的视图)

    有趣的是,在知道这一点之后。。将选定单元格的背景色设置为“清除”仍然无效。。做了更多的研究后,我发现在创建此自定义单元格时,我只需修改选择样式:

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

    这绝对是一个iOS错误。在iOS 8中,在
    Interface Builder
    中设置背景色对iPhone来说很好,但在iPad中总是
    whiteColor
    。要修复此问题,请在
    cellForIndexPath
    数据源消息中,将其放入:

    cell.backgroundColor=cell.backgroundColor

    它会起作用的。这正是为什么我说它是一个bug,因为代码本身几乎是胡说八道,但它是有效的。

    Swift 1.2解决方案:

    只需为单元格的背景色添加一个明确的定义,如 因此:

    为我设置
    cell.backgroundColor=cell.contentView.backgroundColor
    tableView:willDisplayCell:forRowAtIndexPath:
    tableView:cell:forRowAtIndexPath:
    中执行了该作业


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

    也遇到了类似的问题,不得不这样做

  • 将蓝色设置为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
    }
    

  • 不幸的是,这不是真的
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        // Configure the cell...
        cell.backgroundColor = UIColor.clearColor()
    
        return cell
    }
    
    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
    }