UITableViewCell show white background and cannot be modified on iOS7

后端 未结 14 1287
轻奢々
轻奢々 2020-11-28 18:42

I\'ve implemented a custom table view cell class that inherit from UITableViewCell. The tableview contains a background image, so I want cell\'s background to b

相关标签:
14条回答
  • 2020-11-28 18:44

    Had a similar problem, had to

    1. Set blue as the selectionStyle and
    2. add this to the code to correct it

      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
      }
      
    0 讨论(0)
  • 2020-11-28 18:48

    The default background color of an UITableViewCell in iOS 7 is white.

    You have to set the backgroundColor property somewhere in your code. For example, set it after you newly created the cell.

    cell.backgroundColor = [UIColor clearColor];
    
    0 讨论(0)
  • 2020-11-28 18:49

    You can also use colour code in order to make your UITableView Cell lucrative.

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

    This is the code for applying colour code method:

    -(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];
    }
    
    0 讨论(0)
  • 2020-11-28 18:52

    When adding UI objects to a cell, Xcode 5/IOS 7 adds a new "Content View" which will be the superview of all elements in the cell. To set the background color of the cell, set the background color of this Content View. The solutions above didnt work for me but this one worked well for me.

    0 讨论(0)
  • 2020-11-28 18:52

    Swift 1.2 Solution:

    Just add an explicit definition for your cell's background color like so:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        // Configure the cell...
        cell.backgroundColor = UIColor.clearColor()
    
        return cell
    }
    
    0 讨论(0)
  • 2020-11-28 18:53

    I investigated it a little bit and found out that the cell backgroundColor is set by the Appearance system. So if all cells in your application have clear background, the easiest solution would be:

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

    And even if they have different background, clear color seems to be the most convenient as the default one.

    0 讨论(0)
提交回复
热议问题